我正在运行一个相当令人困惑的错误。
我试图提交一个表单嵌套的属性-我通过在Rails 4中的strong_params白名单。
每当我尝试提交表单,我得到这个错误:
ActiveRecord::UnknownAttributeError -未知属性:email:
我的用户模型有以下设置:
user_controller.rb
def update
if @user.profile.update_attributes!(profile_params)
respond_to do |format|
format.js
format.html { redirect_to edit_user_path(@profile.user) }
end
end
end
private
def profile_params
params.require(:user).permit(:email,
{:profile_attributes => [:first_name, :last_name, :website, :birthdate, :description,
{:address_attributes => [:city, :country, :phone]}]}
)
end
这给了我以下参数:
{"电子邮件"=>"martin@teachmeo.com","profile_attributes"=>{"first_name"=>"马丁","last_name"=>"朗","网站"=>","生日"=>","描述"=>"}}
我的用户模型如下:
用户(id: integer, email: string, password_digest: string, created_at: datetime, updated_at: datetime, auth_token: string)
有趣的是,虽然,如果我试图调试它通过撬@user.update_attributes(profile_params)工作没有任何问题。
您正在呼叫
@user.profile.update_attributes!(profile_params)
这意味着您正在更新Profile
(我假设这是模型名称)的实例上的属性, 而不是 User
。正如你所指出的,:email
是User
模型上的一个专栏,而不是 Profile
模型。您正在尝试将键:email
的值应用于@user.profile
, Profile
没有的列,因此出现ActiveRecord::UnknownAttributeError - unknown attribute: email:
错误。
我猜你真正想要的不是上面的
@user.update_attributes!(profile_params)
因为User
有:email
属性,也可能有accepts_nested_attributes_for :profile
设置。