Ruby-on-rails Rails 3 嵌套表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5073698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:29:12  来源:igfitidea点击:

Rails 3 Nested Forms

ruby-on-railsruby-on-rails-3

提问by Mike

I have a Person model and an Address Model:

我有一个人模型和一个地址模型:

class Person < ActiveRecord::Base
  has_one :address
  accepts_nested_attributes_for :address
end


class Address < ActiveRecord::Base
  belongs_to :person
end

In my people controller I have @person.build_addressin my new action. My forms builds correctly. The problem is that when I submit the form, a person record and an address record is created but they aren't linked via the address_id column in the Person table.

在我的人员控制器中,我有@person.build_address我的新动作。我的表单构建正确。问题是,当我提交表单时,会创建一个人员记录和一个地址记录,但它们没有通过 Person 表中的 address_id 列链接。

Am I missing a step in the controller?

我错过了控制器中的一个步骤吗?

Thanks!

谢谢!

New Action UPDATE

新动作 更新

def new
    @person = Person.new
    @person.build_address

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

Form Code UPDATE

表格代码 更新

<%= form_for(@person) do |f| %>
  <% if @person.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@person.errors.count, "error") %> prohibited this person from being saved:</h2>

      <ul>
      <% @person.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %>
  </div>
  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :telephone %><br />
    <%= f.text_field :telephone %>
  </div>
  <div class="field">
    <%= f.label :mobile_phone %><br />
    <%= f.text_field :mobile_phone %>
  </div>
  <div class="field">
    <%= f.label :date_of_birth %><br />
    <%= f.date_select :date_of_birth %>
  </div>
  <div class="field">
    <%= f.label :gender %><br />
    <%= f.select(:gender, Person::GENDER_TYPES) %>
  </div>
  <div class="field">
    <%= f.label :notes %><br />
    <%= f.text_area :notes %>
  </div>
  <div class="field">
    <%= f.label :person_type %><br />
    <%= f.select(:person_type, Person::PERSON_TYPES) %>
  </div>

<%= f.fields_for :address do |address_fields| %>
 <div class="field">
    <%= address_fields.label :street_1 %><br />
    <%= address_fields.text_field :street_1 %>
  </div>
 <div class="field">
    <%= address_fields.label :street_2 %><br />
    <%= address_fields.text_field :street_2 %>
  </div>
<div class="field">
    <%= address_fields.label :city %><br />
    <%= address_fields.text_field :city %>
  </div>
 <div class="field">
    <%= address_fields.label :state %><br />
    <%= address_fields.select(:state, Address::STATES) %>
  </div>
<div class="field">
    <%= address_fields.label :zip_code %><br />
    <%= address_fields.text_field :zip_code %>
  </div>
<% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

回答by Ryan Bigg

You need to have accepts_nested_attributes_for :addresson your Personmodel for this to work nicely. In your createaction you can then do this:

你需要有accepts_nested_attributes_for :address你的Person模型才能很好地工作。在你的create行动中,你可以这样做:

def create
  @person = Person.new(params[:person])
  ...
end

Then Rails will take care of the rest.

然后 Rails 会处理剩下的事情。

UPDATE: if the address_idcolumn is in the peopletable then it should be belongs_to :address, not has_one :address

更新:如果该address_id列在people表中,那么它应该是belongs_to :address,而不是has_one :address

回答by christianblais

Why is your address built in your new action, and not in the create action? You're building an address from a non saved model, without an id, so the foreign key can't be set. You should keep your @person in your new action, but put your build_address in your create action, after @person has been saved.

为什么你的地址建立在你的新操作中,而不是创建操作中?您正在从未保存的模型构建地址,没有 id,因此无法设置外键。在保存@person 之后,您应该将@person 保留在新操作中,但将 build_address 放在创建操作中。