本文介绍了ActiveModel :: ForbiddenAttributesError + cancan + rails 4 +具有范围控制器的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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类:App

def index
end

#CRUD方法和一些其他自定义方法
...

私人

def app_params
params.require(:app).permit(:name,:description,:author,:url_path,:validated,:active,:version)
结束
结束

当我尝试创建应用。

  ActiveModel :: ForbiddenAttributesError-ActiveModel :: ForbiddenAttributesError:
activemodel(4.0.0)lib / active_model / forbidden_​​attributes_protection。 rb:21:in`sanitize_for_mass_assignment'

我添加了

  before_filter做
资源= controller_path.singularize.gsub('/','_')。to_sym
method =#{resource} _params
params [resource]& == send(method)if response_to?(method,true)
end

$ b $ a

,如,但仍出现上述错误。

解决方案

使用名称空格。请尝试将您的代码更改为以下代码。 @JiriKolarik建议他使用名称空间的解决方案后,我遇到了同样的问题。希望对您有所帮助。

  before_filter do 
resource = controller_name.singularize.to_sym
method =#{resource} _params
params [resource]& == send(method)if response_to?(method,true)
end


I m using cancan(1.6.10) with rails 4.0.0. I have a model called 'App'(not scoped) and a controller Admin::AppsController(its scoped. ie app/controllers/admin/apps_controller).

the controller code is as

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

I m getting error when i try to create a 'app'.

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

as specified in https://github.com/ryanb/cancan/issues/835#issuecomment-18663815 but still getting the above error.

解决方案

Using name spaces. Please try to change your code to this one below. I had same issue after @JiriKolarik suggested his solution to work with name spaces. I hope it helps.

  before_filter do
    resource = controller_name.singularize.to_sym
    method = "#{resource}_params"
    params[resource] &&= send(method) if respond_to?(method, true)
  end

这篇关于ActiveModel :: ForbiddenAttributesError + cancan + rails 4 +具有范围控制器的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 01:46