Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: How to use fields_for with two objects of the same class in a single form?
    primarykey
    data
    text
    <p>I'm trying to use <code>form_for</code> and <code>fields_for</code> in Rails 3 to let me edit two objects (of the same class) on one page, but I'm not having any luck: the second object's HTML input tags come out with the same IDs as the first object's. </p> <p>The specific situation:</p> <ul> <li>I have a class <code>Card</code>. I'm trying to follow general RESTful patterns for it.</li> <li>Most Cards stand alone, but a small number of them have a second Card object affiliated with the first one, via the "link" property. (I call these "multipart" Cards.)</li> <li>I want it to be possible to use the standard Card edit page (views/cards/edit.html.erb and views/cards/_form.html.erb) to switch a Card from standalone mode to multipart mode. I'm using JavaScript to show the second set of edit fields on the form when the user selects multipart mode on the edit page. Therefore, the edit form for <em>every</em> Card needs to contain the form fields for a second Card, to potentially become the "link"ed Card from the main Card being edited.</li> <li>The HTML fields for the second Card should be identical to those for the first one. I have a complex pretty HTML layout for my fields and inputs, and I want to keep things DRY and avoid duplicating all that code for the second Card's field.</li> </ul> <p>This turns out to be really problematic. I've tried assorted things like:</p> <pre><code>&lt;%= form_for(@card) do |outer_form| %&gt; ... &lt;% [@card, @card2].each_with_index do |card, card_index| %&gt; &lt;%= fields_for card do |f| %&gt; </code></pre> <p>This doesn't work because the second object's HTML input tags come out with the same IDs as the first object's. They <strong>both</strong> come out with HTML <code>&lt;select id="card_rarity" name="card[rarity]"&gt;</code>, which seems obviously wrong.</p> <p>I tried changing the quoted lines to say</p> <pre><code> &lt;% [@card, @card2].each_with_index do |card, card_index| %&gt; &lt;%= fields_for (card_index == 0 ? card : card.link) do |f| %&gt; </code></pre> <p>with the hope that this would then provide HTML names like "card[link][rarity]". (Edit:) But it doesn't: even with <code>fields_for card.link</code>, it still produces form fields with <code>name="card[rarity]"</code>. So the page has two inputs with the same name and ID, and discards one of them on submission.</p> <p>I tried saying </p> <pre><code> &lt;% [@card, @card2].each_with_index do |card, card_index| %&gt; &lt;%= fields_for card, :as =&gt; (card_index == 0 ? "card" : "card2") do |f| %&gt; </code></pre> <p>but that didn't work either. The :as parameter seemed to be completely ignored.</p> <p>Can anyone suggest the approach that I'm missing here? I don't find the <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for" rel="nofollow">official documentation on form_for and fields_for</a> very helpful.</p> <p>(Edit to add:) In particular, surely there must be a way to edit fields for two different objects of the same class within a single Rails form? <code>fields_for</code> makes it trivial to do this with any number of objects of <em>different</em> classes - but how can it be done for two of the same class?</p> <p>Many thanks!</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.
 

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