开发者

Rails 3: Setting ''role_ids' value to empty only when such a input area is present?

I have my User model, and a user can have many Roles.

In the User form i set roles to the user by doing this:

  <p><% for role in Role.all %>
        <%= check_box_tag "user[roles_ids][]", role.id, @user.roles.include?(role) %>
        <%= role.name %>
     <% end %>
  </p>

and then in the controller i have the following to ensure that its cleared out when the user has no roles selected:

params[:user][:role_ids] ||= []

This all works great, however i want to have an if statement in the controller to only empty out the role_ids when such values are returned, the reason is that for some users the roles addition check boxes are not displayed so they are not allowed to set roles, however when they make changes to other attributes it clears all the prev开发者_运维问答ious associations with any roles that were present before the update.

How can i get around this?

Thanks


Hi I think if you have users with different roles decision either update ids or not should be based on user role and not on form data so you can do sth like

params[ :user ][ :roles_ids ] = current_user.roles_ids unless current_user.has_role? :admin
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜