Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>An even DRYer solution:</p> <p>1) in your Gemfile, list <code>nested_form</code> as a dependency.</p> <p>2) in your models do this:</p> <pre><code>class Account &lt; ActiveRecord::Base accepts_nested_attributes_for :organizations end class Organization &lt; ActiveRecord::Base accepts_nested_attributes_for :locations end </code></pre> <p>3) create regular forms for Organization under <code>./app/view/organizations/ and for Location under</code>./app/view/locations/`</p> <p>4) in your Account form, do this: (it gets pretty short this way!)</p> <pre><code>&lt;%= nested_form_for @account do |f| %&gt; &lt;%= f.label :account_type %&gt;&lt;br /&gt; &lt;%= f.text_field :account_type %&gt;&lt;br /&gt; &lt;%= f.fields_for :organizations %&gt; &lt;%= f.submit "Add account" %&gt; &lt;% end %&gt; </code></pre> <p>5) in your Organization form, do this: (also pretty short)</p> <pre><code>&lt;%= nested_form_for @organization do |f| %&gt; &lt;%= f.label :name %&gt;&lt;br /&gt; &lt;%= f.text_field :name %&gt;&lt;br /&gt; &lt;%= f.label :website %&gt;&lt;br /&gt; &lt;%= f.text_field :website %&gt;&lt;br /&gt; &lt;%= f.fields_for :locations %&gt; &lt;%= f.submit "Add Organization" %&gt; &lt;% end %&gt; </code></pre> <p>Check these RailsCasts:</p> <p><a href="http://railscasts.com/episodes/196-nested-model-form-part-1" rel="nofollow">http://railscasts.com/episodes/196-nested-model-form-part-1</a></p> <p><a href="http://railscasts.com/episodes/197-nested-model-form-part-2" rel="nofollow">http://railscasts.com/episodes/197-nested-model-form-part-2</a></p>
 

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