ActiveModel::ForbiddenAttributesError 创建用户时
ActiveModel::ForbiddenAttributesError when creating user
我正在使用 Rails 5 和 Postgres 9.5。我在提交旨在在我的数据库中创建用户的表单时遇到问题。我的控制器里有这个
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "You signed up successfully"
flash[:color]= "valid"
else
flash[:notice] = "Form is invalid"
flash[:color]= "invalid"
end
render "new"
end
这是我的 table 在我的 Postgres 数据库中的样子……
sims=> \d users;
Table "public.users"
Column | Type | Modifiers
--------------------+-----------------------------+----------------------------------------------------
id | integer | not null default nextval('users_id_seq'::regclass)
username | character varying |
email | character varying |
encrypted_password | character varying |
salt | character varying |
first_name | character varying |
last_name | character varying |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
但是在使用以下参数提交我的表单时,我收到了错误,正如您从我的日志中看到的那样……
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"q4YmPjUhD5olnRRgCt/gUuCDrb0lt+EqOxpzXGdtGHBtkwPEYgyp12H8lF04FHpqrZRZCip1Mo8/tvGQinPpJg==", "user"=>{"username"=>”mysuername”, "email"=>”mysuername@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Signup"}
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
日志中没有其他信息。我还需要做什么才能提交我的表单?
出于安全考虑,Rails 正在阻止保存。本指南有一个简洁明了的解释:http://blog.teamtreehouse.com/rails-4-strong-paremeters
要修复您的具体示例,请将 @user = User.new(params[:user])
行更改为:
@user = User.new(params.require(:user).permit(:username, :email, :password, :password_confirmation))
我正在使用 Rails 5 和 Postgres 9.5。我在提交旨在在我的数据库中创建用户的表单时遇到问题。我的控制器里有这个
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "You signed up successfully"
flash[:color]= "valid"
else
flash[:notice] = "Form is invalid"
flash[:color]= "invalid"
end
render "new"
end
这是我的 table 在我的 Postgres 数据库中的样子……
sims=> \d users;
Table "public.users"
Column | Type | Modifiers
--------------------+-----------------------------+----------------------------------------------------
id | integer | not null default nextval('users_id_seq'::regclass)
username | character varying |
email | character varying |
encrypted_password | character varying |
salt | character varying |
first_name | character varying |
last_name | character varying |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
但是在使用以下参数提交我的表单时,我收到了错误,正如您从我的日志中看到的那样……
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"q4YmPjUhD5olnRRgCt/gUuCDrb0lt+EqOxpzXGdtGHBtkwPEYgyp12H8lF04FHpqrZRZCip1Mo8/tvGQinPpJg==", "user"=>{"username"=>”mysuername”, "email"=>”mysuername@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Signup"}
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
日志中没有其他信息。我还需要做什么才能提交我的表单?
Rails 正在阻止保存。本指南有一个简洁明了的解释:http://blog.teamtreehouse.com/rails-4-strong-paremeters
要修复您的具体示例,请将 @user = User.new(params[:user])
行更改为:
@user = User.new(params.require(:user).permit(:username, :email, :password, :password_confirmation))