[英]ActiveModel ForbiddenAttributesError
請幫幫我。 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? && !@user.new_record?)
end
“ config.action_controller.permit_all_parameters = true”解決了該問題。 但是我不想禁用strong_parameters。
更新
app / 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? && !@user.new_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
附言我從事本指南工作 。 問題與該問題非常相似: 使用Active Admin和Devise會收到ActiveModel :: ForbiddenAttributesError當在管理面板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"=>"test@test.com", "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)>'
我必須同意您所參考問題的答案, 根據文檔,請嘗試:
permit_params :email, :password, :password_confirmation, :superadmin
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.