Topic: fields_for, tables and the hidden id field
Hey guys.
Suppose I have a collection of objects that I'm iterating over, and I'm wanting the user to enter data for the fields. Suppose I'm also outputting the fields in to a table.
Here's the code I'm using:
<% f.fields_for :things do |things_form| %>
<tr class="<%= cycle("odd","even") %>">
<td>
<%= things_form.text_field :description, :size => 30 %>
</td>
<td>
<%= things_form.text_field :value %>
</td>
</tr>
<% end %>Now, because fields_for automatically spits out the hidden input field for the ID, this is the resultant HTML (with the surrounding table included):
<table cellspacing="1" cellpadding="2">
<thead>
<tr>
<th>Description</th>
<th>Current value</th>
</tr>
</thead>
<tbody>
<input id="application_thing_attributes_0_id" name="application[thing_attributes][0][id]" type="hidden" value="36912">
<tr>
<td>
<input id="application_thing_attributes_0_description" maxlength="255" name="application[thing_attributes][0][description]" value="Blah">
</td>
<td>
<input id="application_thing_attributes_0_asset_value" maxlength="15" name="application[thing_attributes][0][asset_value]" size="10" type="text" value="400">
</td>
</tr>
<input id="application_thing_attributes_1_id" name="application[thing_attributes][1][id]" type="hidden" value="36913">
<tr>
<td>
<input id="application_thing_attributes_1_description" maxlength="255" name="application[thing_attributes][1][description]" size="30" type="text" value="">
</td>
<td>
<input id="application_thing_attributes_1_asset_value" maxlength="15" name="application[thing_attributes][1][asset_value]" size="10" type="text" value="0">
</td>
</tr>
</tbody>
</table>If you look closely, the HTML is invalid because there is an <input> element nested underneath <tbody>.
How can I explicitly set where the ID field is rendered so that the fields_for loop will render valid HTML?