0

Suppose we have User ActiveRecord model.

class User < ActiveRecord::Base
  has_many :users_roles, dependent: :destroy
end

And Users Roles ActiveRecord model.

class UsersRole < ActiveRecord::Base
  enum role: { guest: 0,
               os_employee: 1,
               os_contractor: 2,
               os_moderator: 3,
               os_administrator: 4,
               learner: 5,
               learner_representative: 6,
               teacher: 7,
               head_teacher: 8,
               system_administrator: 9,
               observer: 10  }
end

How to create a form and a corresponding controller action that will allow us to change a user roles? The form can be with a user form with nested roles attributes or stand alone form for users roles list.

Dmitry Shvetsov
  • 651
  • 10
  • 19

2 Answers2

1

You can use nested attributes using build @user.roles.build

= f.fields_for :roles do |r|

   = r.select(:role, options_for_select(Role.pluck(:role)))
0

Not clear why you need a specific controller action for this.

You can add directly in the view, as one field of your form:

f.select :role, UsersRole.roles.keys.map { |r| [r.humanize, r] }

(check Saving enum from select in Rails 4.1 )

Community
  • 1
  • 1
MrWater
  • 1,797
  • 4
  • 20
  • 47
  • A controller action to handle the form submit – Dmitry Shvetsov Feb 21 '17 at 13:38
  • Would you please paste your code of current view and controller, so we may have a clear understanding of what you are trying to do? (I see this was from last september - is it still an issue ? ) What I mean is that your default controller action should handle this. You will probably need to add role field to the permits – MrWater Feb 21 '17 at 15:13