以多模型形式链接两个模型 [英] Linking two models in a multi-model form

查看:37
本文介绍了以多模型形式链接两个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个嵌套的多模型表单,使用用户和配置文件.

用户有_一个个人资料,个人资料属于用户.

提交表单时,会创建一个新用户,并创建一个新配置文件,但它们没有链接(这是第一个明显的问题).用户的模型有一个 profile_id 行,而个人资料的模型有一个 user_id 行.

这是表格的代码:

<%= form_for(@user, :url => team_path) do |f|%><p><%= f.label :email %><br/><%= f.text_field :email %></p><p><%= f.label :password %><br/><%= f.password_field :password %></p><p><%= f.label :password_confirmation %><br/><%= f.password_field :password_confirmation %></p><%= f.hidden_​​field :role_id, :value =>@role.id %></p><%= f.hidden_​​field :company_id, :value =>current_user.company_id %></p><%= fields_for @user.profile do |profile_fields|%><div class="field"><%= profile_fields.label :first_name %><br/><%= profile_fields.text_field :first_name %>

<div class="field"><%= profile_fields.label :last_name %><br/><%= profile_fields.text_field :last_name %>

<%结束%><p><%= f.submit注册"%></p><%结束%>

第二个问题是,即使通过用户模型的表单成功创建了用户名和密码,隐藏字段(role_id & company_id - 它们也是指向其他模型的链接)并未创建(即使它们是模型的一部分)- 但是,这些字段的值已成功显示在 HTML 中.

任何帮助都会很棒!

根据要求,控制器代码:

 def new@user = User.new@user.profile = Profile.new@role = Role.find_by_name("普通")response_to do |格式|format.html # index.html.erbformat.xml { 渲染:xml =>@团队}结尾结尾定义创建@user = User.new(params[:user])@profile = Profile.new(params[:profile])response_to do |格式|如果@profile.save &&@user.saveformat.html { redirect_to (teams_path) }format.xml { 渲染:xml =>@profile, :status =>:created, :location =>@轮廓 }别的format.html { 渲染:动作 =>新的" }format.xml { 渲染:xml =>@profile.errors, :status =>:unprocessable_entity }结尾结尾结尾

解决方案

要回答第一个问题,请更改以下内容:

@profile = Profile.new(params[:profile])

@profile = @user.profile.build(params[:profile]) #has_many关系的情况

@profile = @user.build_profile(params[:profile]) #has_one关系的情况

构建命令构建一个正确设置 user_id 的新配置文件.

对于第二个问题,您能否在 new 操作期间删除对 Role 和 Company 的查询,而在 create 操作期间分配这些查询?这将消除传递隐藏参数的必要性.

I have a nested multimodel form right now, using Users and Profiles.

Users has_one profile, and Profile belongs_to Users.

When the form is submitted, a new user is created, and a new profile is created, but they are not linked (this is the first obvious issue). The user's model has a profile_id row, and the profile's model has a user_id row.

Here is the code for the form:

<%= form_for(@user, :url => teams_path) do |f| %>


  <p><%= f.label :email %><br />
  <%= f.text_field :email %></p>

  <p><%= f.label :password %><br />
  <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>

  <%= f.hidden_field :role_id, :value => @role.id %></p>
  <%= f.hidden_field :company_id, :value => current_user.company_id %></p>

  <%= fields_for @user.profile do |profile_fields| %>

    <div class="field">
    <%= profile_fields.label :first_name %><br />
    <%= profile_fields.text_field :first_name %>
  </div>
  <div class="field">
    <%= profile_fields.label :last_name %><br />
    <%= profile_fields.text_field :last_name %>
  </div>

  <% end %>


  <p><%= f.submit "Sign up" %></p>
<% end %>

A second issue, is even though the username, and password are successfully created through the form for the user model, the hidden fields (role_id & company_id - which are also links to other models) are not created (even though they are part of the model) - the values are successfully shown in the HTML for those fields however.

Any help would be great!

As requested, the controller code:

  def new
    @user = User.new
    @user.profile = Profile.new
    @role = Role.find_by_name("Regular")

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @teams }
    end
  end

  def create

    @user = User.new(params[:user])
    @profile = Profile.new(params[:profile])

    respond_to do |format|
      if @profile.save && @user.save
        format.html { redirect_to (teams_path) }
        format.xml  { render :xml => @profile, :status => :created, :location => @profile }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @profile.errors, :status => :unprocessable_entity }
      end
    end
  end

解决方案

To answer question number one, change the following:

@profile = Profile.new(params[:profile])

to

@profile = @user.profile.build(params[:profile]) #In the case of a has_many relationship

or

@profile = @user.build_profile(params[:profile]) #In the case of a has_one relationship

The build command builds a new profile with the user_id properly set.

For the second question, can you delete the query for Role and Company during the new action and instead assign those during the create action? This would remove the necessity of passing hidden parameters.

这篇关于以多模型形式链接两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆