我使用 cancan (1.6.10) 和 devise (3.2.2),我一直在使用本指南 https://github.com/ryanb/cancan/wiki/Role-Based-Authorization#wiki-many-roles-per-user根据cancan作者的建议,我需要为一个用户分配多个角色,然后我决定使用位掩码将其存储到单个整数列中(我在用户模型中添加了一个名为“roles_mask”的列)。
我涉及到这些文件:
-
user.rb https://gist.github.com/geoom/9340643
- 编辑.html.erb https://gist.github.com/geoom/9340635
我知道我逐字遵循了本指南,除了我编写的行表明角色是一个可访问的属性:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :roles) }
end
end
更新用户(已记录)时,除 :roles_mask =/ 之外的所有字段都会更新,我不明白为什么不捕获角色字段。我想有些东西是我无法看到的。有人可以帮助我吗?
*Solution:
Using 康康康 https://github.com/CanCanCommunity/cancancan(支持 Rails 4)并更改 application_controller.rb 文件它可以工作(因为角色是非标量属性)。
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, roles: []) }
end
end