Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 Nested Forms
    primarykey
    data
    text
    <p>I have a Person model and an Address Model:</p> <pre><code>class Person &lt; ActiveRecord::Base has_one :address accepts_nested_attributes_for :address end class Address &lt; ActiveRecord::Base belongs_to :person end </code></pre> <p>In my people controller I have <code>@person.build_address</code> in 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.</p> <p>Am I missing a step in the controller?</p> <p>Thanks!</p> <p>New Action <strong>UPDATE</strong></p> <pre><code>def new @person = Person.new @person.build_address respond_to do |format| format.html # new.html.erb format.xml { render :xml =&gt; @person } end end </code></pre> <p>Form Code <strong>UPDATE</strong></p> <pre><code>&lt;%= form_for(@person) do |f| %&gt; &lt;% if @person.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@person.errors.count, "error") %&gt; prohibited this person from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @person.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="field"&gt; &lt;%= f.label :first_name %&gt;&lt;br /&gt; &lt;%= f.text_field :first_name %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :last_name %&gt;&lt;br /&gt; &lt;%= f.text_field :last_name %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :email %&gt;&lt;br /&gt; &lt;%= f.text_field :email %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :telephone %&gt;&lt;br /&gt; &lt;%= f.text_field :telephone %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :mobile_phone %&gt;&lt;br /&gt; &lt;%= f.text_field :mobile_phone %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :date_of_birth %&gt;&lt;br /&gt; &lt;%= f.date_select :date_of_birth %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :gender %&gt;&lt;br /&gt; &lt;%= f.select(:gender, Person::GENDER_TYPES) %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :notes %&gt;&lt;br /&gt; &lt;%= f.text_area :notes %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :person_type %&gt;&lt;br /&gt; &lt;%= f.select(:person_type, Person::PERSON_TYPES) %&gt; &lt;/div&gt; &lt;%= f.fields_for :address do |address_fields| %&gt; &lt;div class="field"&gt; &lt;%= address_fields.label :street_1 %&gt;&lt;br /&gt; &lt;%= address_fields.text_field :street_1 %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= address_fields.label :street_2 %&gt;&lt;br /&gt; &lt;%= address_fields.text_field :street_2 %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= address_fields.label :city %&gt;&lt;br /&gt; &lt;%= address_fields.text_field :city %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= address_fields.label :state %&gt;&lt;br /&gt; &lt;%= address_fields.select(:state, Address::STATES) %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= address_fields.label :zip_code %&gt;&lt;br /&gt; &lt;%= address_fields.text_field :zip_code %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload