【问题标题】:ActiveModel::ForbiddenAttributes errorActiveModel::ForbiddenAttributes 错误
【发布时间】:2015-01-07 12:43:21
【问题描述】:

我做了以下事情。我安装了 strong_parameters gem。

1)我创建了一个初始化器并添加了以下行 ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)

2)我从模型中删除了 attr_accessible。

然后我试图创建一个新记录,它不会去创建方法本身。它给出了 ActiveModel::ForbiddenAttributes 错误。请解释一下可能是什么问题?

请在下面找到代码:

config/initializers/strong_parameters.rb

 ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)

app/models/role.rb

 class Role < ActiveRecord::Base
  #attr_accessible :name, :description
  validates :name, presence: true, uniqueness: { case_sensitive: false}
 end

app/controllers/roles_controller.rb

 class RolesController < ApplicationController

  def create
   @role = Role.new(role_params)
   if @role.save
     redirect_to roles_path, notice: t('Role was successfully created.', default: 'Role was successfully created.')
  else
     render action: "new"
  end
 end

 private

 def role_params
  params.require(:role).permit(:name,:description)
 end

end

编辑:请查找错误信息:

【问题讨论】:

  • log发布您的params
  • @RajarshiDas 我已经用错误消息更新了问题。

标签: ruby-on-rails ruby ruby-on-rails-3 strong-parameters


【解决方案1】:

cancan gem 有问题。我尝试了下面的skip_load_resource。它现在正在工作。

 load_and_authorize_resource skip_load_resource only: [:create] 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多