一对一关系问题 - Rails

发布于 2024-11-28 07:20:12 字数 1118 浏览 1 评论 0原文

我的用户和合作伙伴之间存在一对一的关系。在我的用户模型中,我有以下内容:

 has_one :partner, :class_name => "Partner"

如果“relation_status == Coupled”,我想创建一个新的合作伙伴,然后让用户转到一个新表单来填写有关他/她的合作伙伴的信息。因此,在我的 UsersController 中,我有

respond_to do |format|
    if @user.save && @user.relation_status != "Coupled"
           format .html { redirect_to @user, notice: "User was successfully created ID   
      was #{@id} "}
           format.json { render json: @user, status: :created, location: @user }

         elsif @user.save && @user.relation_status == "Coupled"
           format .html { redirect_to partner_signup_path, notice: "Time for your patna"     
            }
           format.json { render json: @user, status: :created, location: @user }
    else
           format.html { render action: "new" }
           format.json { render json: @user.errors, status: :unprocessable_entity }
     end
   end
 end

在 PartnersController 中,我有基本的新建操作和创建操作,可以成功创建新的合作伙伴。显然,通过执行上面的操作,无法将合作伙伴的 ID 自动分配给用户的外键 -partner_id。我怎样才能正确地做到这一点?我应该在用户模型中执行此操作吗?我是一个 Rails 新手。感谢您的帮助。

I have a one-to-one relationship between a User and a Partner. In my User model, I have the following:

 has_one :partner, :class_name => "Partner"

I want to create a new Partner if "relation_status == coupled" and then make the user go to a new form to fill out information on his/her Partner. So in my UsersController, I have

respond_to do |format|
    if @user.save && @user.relation_status != "Coupled"
           format .html { redirect_to @user, notice: "User was successfully created ID   
      was #{@id} "}
           format.json { render json: @user, status: :created, location: @user }

         elsif @user.save && @user.relation_status == "Coupled"
           format .html { redirect_to partner_signup_path, notice: "Time for your patna"     
            }
           format.json { render json: @user, status: :created, location: @user }
    else
           format.html { render action: "new" }
           format.json { render json: @user.errors, status: :unprocessable_entity }
     end
   end
 end

In my PartnersController, I have the basic new and create actions that successfully create a new Partner. Obviously, there's no way to have the Partner's ID assigned to the User's foreign key - partner_id automatically by doing what I did above. How can I go about doing this correctly? Should I be doing this in the User model instead? I'm kind of a Rails newbie. Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2024-12-05 07:20:12

答案是让用户在 UsersController 中保持会话状态 ->会话[:id] = @user.id 。
然后在 PartnersController 中,我做了:
@partner = Partner.new(params[:user])
@partner.save
@user = User.find(会话[:id])
@user.partner = @partner

瞧。

The answer is to keep the user in session in the UsersController -> session[:id] = @user.id .
Then in the PartnersController, I did:
@partner = Partner.new(params[:user])
@partner.save
@user = User.find(session[:id])
@user.partner = @partner

Voila.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文