请帮帮我。 rails 发誓... 我应该改变什么?我允许所有参数(permit_params),但这没有帮助:
ActiveModel::ForbiddenAttributesError
Extracted source (around line #17):
@user = User.where(id: params[:id]).first_or_create
@user.superadmin = params[:user][:superadmin]
@user.attributes = params[:user].delete_if do |k, v|
(k == "superadmin") ||
(["password", "password_confirmation"].include?(k) && v.empty? && <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e8c9a89d9b8d9ac6868d9f" rel="noreferrer noopener nofollow">[email protected]</a>_record?)
end
“config.action_controller.permit_all_parameters = true”解决了问题。但我不想禁用strong_parameters。
更新
应用程序/admin/user.rb
ActiveAdmin.register User do
form do |f|
f.inputs "User Details" do
f.input :email
f.input :password
f.input :password_confirmation
f.input :superadmin, :label => "Super Administrator"
end
f.actions
end
create_or_edit = Proc.new {
@user = User.where(id: params[:id]).first_or_create
@user.superadmin = params[:user][:superadmin]
@user.attributes = params[:user].delete_if do |k, v|
(k == "superadmin") ||
(["password", "password_confirmation"].include?(k) && v.empty? && <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7e687b2b4a2b5e9a9a2b0" rel="noreferrer noopener nofollow">[email protected]</a>_record?)
end
if @user.save
redirect_to :action => :show, :id => @user.id
else
render active_admin_template((@user.new_record? ? 'new' : 'edit') + '.html.erb')
end
}
member_action :create, :method => :post, &create_or_edit
member_action :update, :method => :put, &create_or_edit
permit_params :authenticity_token, :commit, :id, user: [:email, :password, :password_confirmation, :superadmin]
end
附注我致力于此guide 。 该问题与此问题非常相似:I get ActiveModel::ForbiddenAttributesError with Active Admin and Devise当我在管理面板 ActiveAdmin 中创建新用户时,出现此错误。
更新1
控制台
Started POST "/admin/users" for 127.0.0.1 at 2017-12-08 22:57:04 +0300
Processing by Admin::UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"***********", "user"=>{"email"=>"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="087c6d7b7c487c6d7b7c266b6765" rel="noreferrer noopener nofollow">[email protected]</a>", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "superadmin"=>"0"}, "commit"=>"Create User"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
(3.0ms) BEGIN
(0.4ms) ROLLBACK
Completed 500 Internal Server Error in 24ms (ActiveRecord: 5.2ms)
ActiveModel::ForbiddenAttributesError
ActiveModel::ForbiddenAttributesError):
app/admin/user.rb:17:in `block (2 levels) in <top (required)>'
最佳答案
我必须同意您提到的问题的答案,per the doc请尝试:
permit_params :email, :password, :password_confirmation, :superadmin
关于ruby-on-rails - ActiveModel 禁止属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47706276/