当前位置: 动力学知识库 > 问答 > 编程问答 >

roles - Rails collection_select does not save all values in params hash

问题描述:

So, I'm trying to implement simple role based authentication system using Rails and I'm having a problem with final step - changing roles.

role is attribute in user table and it has a string type.

Idea is that some users with some privilages have ability to change roles.

Code in view looks like this:

<div>

<%= f.label :role, 'Role' %>

<%= f.collection_select :role, User::ROLES, :to_s, :humanize,

prompt: 'Select role' %>

</div>

update method in users_controller looks like this:

def update

@user = User.find(params[:id])

if @user.update(update_params) #update_params is method that returns permitted parameters

redirect_to @user

else

render :edit

end

end

Problem is that user[role] is empty after submitting a form.

Everything is pretty much made "by the book". Also, I am using Cancan but it's turned off for edit and update with load_and_authorize_resource :except => [:update, :edit].

网友答案:

So, I've found a solution. Since I'm using CanCan and user_params method so CanCan itself can set permitted parameters, my update_params method is ignored.

Solution is to add :role to user_params method.

分享给朋友:
您可能感兴趣的文章:
随机阅读: