我有两个模型用户和配置文件。
我想将用户名和密码保存在配置文件中的用户和其他用户配置文件详细信息中。
现在,
用户模型具有:在Rails3中嵌套模型
has_one :profile
accepts_nested_attributes_for :profile
attr_accessible :email, :password
轮廓模型具有
belongs_to :user
attr_accessible :bio, :birthday, :color
用户控制器已经
def new
@user = User.new
@profile = @user.build_profile
end
def create
@user = User.new(params[:user])
@profile = @user.create_profile(params[:profile])
if @user.save
redirect_to root_url, :notice => "user created successfully!"
else
render "new"
end
end
视图new.html.erb具有用于字段用户和配置文件。
然而,当我运行这个Web应用程序是显示错误:
不能大规模指派保护的属性:简介
上调试它停留在@user = User.new(PARAMS [:用户] )中创建动作
那么,出了什么问题?我也试过把profile_attributes放在attr_accessible中,但它没有帮助!
请帮我找出解决方案。
尝试删除'@profile = @ user.create_profile(params [:profile])'行。你不需要它。 – 2012-08-13 06:28:25
它看起来像你想要传递给@profile的配置文件实际上是用户参数,因此你的用户表单有问题 – megas 2012-08-13 06:35:13
这告诉我你的视图有问题。你的params散列应该有一个':profile_attributes'而不是':profile'键。批量分配失败,因为您没有'profile'属性并且无法访问。如果您在视图中调用fields_for,请确保将它传递给配置文件的模型。可能是'@profile'或'@user.profile',而不是简单的字符串或符号。 – Joeyjoejoejr 2012-08-13 07:24:36