我在一个相当混乱的错误中运行。
我正在尝试提交一个带有嵌套属性的表单--我正在通过Rails 4中的strong_params将这些属性白化。
每当我试图提交表单时,我都会得到以下错误:
ActiveRecord::不明属性: 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"=>"Martin“、"last_name"=>"Lang”、“网站”“=>”、“生日”“=>”、“description”“=>”}}
我的用户模型看起来如下:
用户(id:整型,电子邮件: string,password_digest: string,created_at: datetime,updated_at: datetime,auth_token: string)
有趣的是,如果我试图通过撬来调试它,@user.update_attributes(profile_params)就能正常工作,不会出现任何问题。
发布于 2013-04-17 04:19:07
你在打电话
@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集。
https://stackoverflow.com/questions/16051241
复制相似问题