ActiveModel::ForbiddenAttributes 错误
ActiveModel::ForbiddenAttributes error
我做了以下事情。我安装了 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
编辑 :请查找错误信息:
康康舞有问题gem。我尝试了以下 skip_load_resource。现在可以使用了。
load_and_authorize_resource skip_load_resource only: [:create]
我做了以下事情。我安装了 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
编辑 :请查找错误信息:
康康舞有问题gem。我尝试了以下 skip_load_resource。现在可以使用了。
load_and_authorize_resource skip_load_resource only: [:create]