【发布时间】:2014-03-04 05:47:47
【问题描述】:
我使用 cancan (1.6.10) 和 devise (3.2.2),我一直按照 cancan 作者的建议使用 this guide 实现授权,我需要为用户分配多个角色,然后我决定将其存储到使用位掩码的单个整数列(我在用户模型中添加了一个名为“roles_mask”的列)。
我有这些文件:
我知道我是逐字阅读本指南的,除了我写的那些表明角色是可访问属性的行:
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 =/,我不明白为什么没有捕获角色字段。我认为有些东西是我看不到的。谁能帮帮我?
*解决方案:
使用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
【问题讨论】:
标签: ruby ruby-on-rails-4 devise cancan