ruby - Adding Role dynamically through Form USing Rolify along with Devise and Cancan -
i followed tutorial "https://github.com/eppo/rolify/wiki/tutorial" nice , working fine. question can't add role through form out using rails console.
<div class="field"><%= user_form.label :email %><br /> <%= user_form.email_field :email %></div> <div class="field"><%= user_form.label :password %><br /> <%= user_form.password_field :password %></div> <div class="field"><%= user_form.label :password_confirmation %><br /> <%= user_form.password_field :password_confirmation %></div> <div class="field"> <%= f.label :roles %> <div class="controls"> <% role.all.each |role| %> <%= check_box_tag "user[role_ids][]", role.id, @user.role_ids.include?(role.id) %> <%= role.name %><br /> <% end %> </div> </div> <% end %>
the role column connect roles table (rolify roles)
here role.rb
class role < activerecord::base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
user.rb
class user < activerecord::base
belongs_to :account, :inverse_of => :users
validates :account, :presence => true
rolify
attr_accessible :role_ids
# include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
# setup accessible (or protected) attributes model
attr_accessible :email, :password, :password_confirmation, :remember_me, :role_ids
# attr_accessible :title, :body
has_many :auditinits
end
any appreciate!!
in user form, make drop down select roles as,
<%= user_form.select :role,options_from_collection_for_select(role.all,"name","name) %>
modify create action in users controller as
@user = user.new(user_params) @user.add_role params[:user][:role]
Comments
Post a Comment