我正在使用 cancan(1.6.10) 和 Rails 4.0.0。我有一个名为“App”(未限定范围)的模型和一个控制器 Admin::AppsController(其限定范围。即 app/controllers/admin/apps_controller)。
控制器代码如下
class Admin::AppsController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource class: App
def index
end
#CRUD methods and some other custom methods
...
private
def app_params
params.require(:app).permit(:name, :description, :author, :url_path, :validated, :active, :version)
end
end
当我尝试创建“应用程序”时出现错误。
ActiveModel::ForbiddenAttributesError - ActiveModel::ForbiddenAttributesError:
activemodel (4.0.0) lib/active_model/forbidden_attributes_protection.rb:21:in `sanitize_for_mass_assignment'
I added
before_filter do
resource = controller_path.singularize.gsub('/', '_').to_sym
method = "#{resource}_params"
params[resource] &&= send(method) if respond_to?(method, true)
end
如指定https://github.com/ryanb/cancan/issues/835#issuecomment-18663815 https://github.com/ryanb/cancan/issues/835#issuecomment-18663815但仍然出现上述错误。