Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're forgetting the to call fields_for as a method on the person form. Otherwise you're not actually using fields_for in a accept_nested_attributes_for context. Michael's solution tries to trick Rails into treating the submission as a properly defined accepts_nested_attributes_for form.</p> <p>The correct syntax for what you are trying to do is:</p> <pre><code>parent_form_object.fields_for id, object_containing_values, {form_for options}, &amp;block </code></pre> <p>You'll find the code looks cleaner and simpler to debug if you provide a symbol as id, containing the association name of the child model as defined in your Person model.</p> <p>Also, the each block you're using might cause problems if @person.phone_numbers is empty. You can ensure that there is at least one set of Phone Number fields with a line similar to the one I used with </p> <pre><code>&lt;% @phs = @person.phone_numbers.empty? ? @person.phone_numbers.build : @person.phone_numbers %&gt; </code></pre> <p>With all corrections, this code will do what you want it to.</p> <p><strong>View</strong></p> <pre><code>&lt;% form_for @person, :builder =&gt; CustomFormBuilder do |f| %&gt; &lt;%= f.error_messages %&gt; &lt;%= f.text_field :first_name %&gt; &lt;%= f.text_field :last_name %&gt; &lt;% f.fields_for :address, @person.address, :builder =&gt; CustomFormBuilder do |address_form| %&gt; &lt;%= address_form.text_field :address_1 %&gt; &lt;%= address_form.text_field :address_2 %&gt; &lt;%= address_form.text_field :city %&gt; &lt;%= address_form.text_field :state %&gt; &lt;%= address_form.text_field :zip %&gt; &lt;% end %&gt; &lt;h2&gt;Phone Numbers&lt;/h2&gt; &lt;% @phs = @person.phone_numbers.empty? ? @person.phone_numbers.build : @person.phone_numbers %&gt; &lt;% f.fields_for :phone_numbers, @phs, :builder =&gt; CustomFormBuilder do |phone_number_form| %&gt; &lt;%= phone_number_form.text_field :description %&gt; &lt;%= phone_number_form.text_field :number %&gt; &lt;%= phone_number_form.text_field :extension %&gt; &lt;% end %&gt; &lt;%= f.text_field :email %&gt; &lt;%= f.submit 'Create' %&gt; &lt;% end %&gt; </code></pre> <p>You might find it useful to check out the <a href="http://github.com/alloy/complex-form-examples" rel="nofollow noreferrer">complex-form-examples repository on github</a> for a working example. It also comes with code to dynamically add new entries for :has_many relationships from the view/form.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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