[英]ROR: Updating two models from one form
我的表单上有两个模型配置文件和用户。 创建用户后,他可以继续编辑其个人资料。 意见很好。 但是,当我单击“保存”以更新编辑的配置文件时。 它没有更新,但是闪存通知显示该配置文件已更新。 可能是什么问题? 我不确定出了什么问题。 下面是代码。
class ProfilesController < ApplicationController
def new
#@user.profile = Profile.new
@user = User.find(current_user.id)
@identity = @user.profile || @user.build_profile()
@identity.save
end
def update
@user = current_user
@identity = @user.profile
if @identity.update_attributes(params[:identity])
flash[:notice] = 'Profile was successfully updated.'
redirect_to(new_profile_path())
else
render :action => "new"
end
end
end
class UsersController < ApplicationController
def show
@user = current_user
@user = User.find(params[:id])
@identity = @user.profile || @user.build_profile()
@identity.save
end
......
end
谢谢你的协助。
这里可能存在一些错误。 但是,解决此问题的最佳方法是简化并使用内置的rails功能来编辑关联。
我建议做的是使用嵌套属性, Ryan Daigle上有一篇很棒的文章 。
我不确定为什么您会在一个new
操作中而不是在create
调用save
,这感觉不对。 还要检查您提交的表单中的模型名称是否是identity
而不是user
或profile
。
没有配置文件的用户可以存在吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.