<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - fields_for, tables and the hidden id field]]></title>
		<link>http://railsforum.com/viewtopic.php?id=39640</link>
		<description><![CDATA[The most recent posts in fields_for, tables and the hidden id field.]]></description>
		<lastBuildDate>Mon, 11 Mar 2013 04:46:54 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: fields_for, tables and the hidden id field]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=161042#p161042</link>
			<description><![CDATA[<p>I don&#039;t understand this topic.</p><br /><br /><br /><p><a href="http://www.bigwigjackets.com/minority-report-leather-jacket.html">bigwigjackets</a> // <a href="http://www.bigwigjackets.com/mission-impossible-leather-jacket-new-batch.html">Mission Impossible</a> // <a href="http://www.bigwigjackets.com/nicolas-cage-jacket.html">bigwigjackets.com</a></p>]]></description>
			<author><![CDATA[dummy@example.com (jame22)]]></author>
			<pubDate>Mon, 11 Mar 2013 04:46:54 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=161042#p161042</guid>
		</item>
		<item>
			<title><![CDATA[Re: fields_for, tables and the hidden id field]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=145456#p145456</link>
			<description><![CDATA[<p>Thank you for solving it!</p>]]></description>
			<author><![CDATA[dummy@example.com (cqpx)]]></author>
			<pubDate>Fri, 07 Oct 2011 01:08:51 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=145456#p145456</guid>
		</item>
		<item>
			<title><![CDATA[Re: fields_for, tables and the hidden id field]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=129243#p129243</link>
			<description><![CDATA[<p>For those interested, here is the relevant Rails method that governs the behaviour of generating these nested fields:</p><p><a href="http://apidock.com/rails/ActionView/Helpers/FormBuilder/fields_for_nested_model">ActionView::Helpers::FormBuilder#fields_for_nested_model</a></p><p>You will notice that the template is run on the block passed to it (ie: your ERB code) first, and once that&#039;s done it emits a hidden ID field. However, it only emits that ID field if it isn&#039;t a new record, and hasn&#039;t already been done manually.</p><p>The associated methods that lets Rails figure out if it needs to create a hidden ID field are:</p><p><a href="http://apidock.com/rails/ActionView/Helpers/FormBuilder/emitted_hidden_id%3F">ActionView::Helpers::FormBuilder#emitted_hidden_id?</a><br /><a href="http://apidock.com/rails/ActionView/Helpers/FormBuilder/hidden_field">ActionView::Helpers::FormBuilder#hidden_field</a></p>]]></description>
			<author><![CDATA[dummy@example.com (Dazza)]]></author>
			<pubDate>Wed, 11 Aug 2010 00:39:42 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=129243#p129243</guid>
		</item>
		<item>
			<title><![CDATA[Re: fields_for, tables and the hidden id field]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=129242#p129242</link>
			<description><![CDATA[<p>OK, so it turns out that Rails 2.3.5 makes this pretty dead simple:</p><div class="codebox"><pre><code>&lt;% f.fields_for :things do |things_form| %&gt;
      &lt;tr class=&quot;&lt;%= cycle(&quot;odd&quot;,&quot;even&quot;) %&gt;&quot;&gt;
        &lt;td&gt;
          &lt;%= things_form.hidden_field :id %&gt;
          &lt;%= things_form.text_field :description, :size =&gt; 30 %&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;%= things_form.text_field :value %&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;% end %&gt;</code></pre></div><p>When you explicitly place the hidden ID field, Rails will oblige and insert the ID field for you at the location you specify.</p><div class="codebox"><pre><code>&lt;table cellspacing=&quot;1&quot; cellpadding=&quot;2&quot;&gt;
    &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Description&lt;/th&gt;
      &lt;th&gt;Current value&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;

    &lt;tbody&gt;

      &lt;tr&gt;
        &lt;td&gt;
          &lt;input id=&quot;application_thing_attributes_0_id&quot; name=&quot;application[thing_attributes][0][id]&quot; type=&quot;hidden&quot; value=&quot;36912&quot;&gt;
          &lt;input id=&quot;application_thing_attributes_0_description&quot; maxlength=&quot;255&quot; name=&quot;application[thing_attributes][0][description]&quot; value=&quot;Blah&quot;&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;input id=&quot;application_thing_attributes_0_asset_value&quot; maxlength=&quot;15&quot; name=&quot;application[thing_attributes][0][asset_value]&quot; size=&quot;10&quot; type=&quot;text&quot; value=&quot;400&quot;&gt;
        &lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td&gt;
          &lt;input id=&quot;application_thing_attributes_1_id&quot; name=&quot;application[thing_attributes][1][id]&quot; type=&quot;hidden&quot; value=&quot;36913&quot;&gt;
          &lt;input id=&quot;application_thing_attributes_1_description&quot; maxlength=&quot;255&quot; name=&quot;application[thing_attributes][1][description]&quot; size=&quot;30&quot; type=&quot;text&quot; value=&quot;&quot;&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;input id=&quot;application_thing_attributes_1_asset_value&quot; maxlength=&quot;15&quot; name=&quot;application[thing_attributes][1][asset_value]&quot; size=&quot;10&quot; type=&quot;text&quot; value=&quot;0&quot;&gt;
        &lt;/td&gt;
      &lt;/tr&gt;

    &lt;/tbody&gt;
&lt;/table&gt;</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (Dazza)]]></author>
			<pubDate>Wed, 11 Aug 2010 00:34:38 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=129242#p129242</guid>
		</item>
		<item>
			<title><![CDATA[Re: fields_for, tables and the hidden id field]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=129241#p129241</link>
			<description><![CDATA[<p>So I left this alone for a while and came back to the problem recently.&nbsp; After a little scrutiny on how our Rails application was outputting forms, it turned out that there was a custom initializer that was overriding the default FormBuilder for Rails. The overridden implementation was spitting out the hidden ID fields before the template was even parsed, which is why I was seeing the ID fields above the table rows.</p>]]></description>
			<author><![CDATA[dummy@example.com (Dazza)]]></author>
			<pubDate>Wed, 11 Aug 2010 00:32:54 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=129241#p129241</guid>
		</item>
		<item>
			<title><![CDATA[fields_for, tables and the hidden id field]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=126838#p126838</link>
			<description><![CDATA[<p>Hey guys.</p><p>Suppose I have a collection of objects that I&#039;m iterating over, and I&#039;m wanting the user to enter data for the fields. Suppose I&#039;m also outputting the fields in to a table.</p><p>Here&#039;s the code I&#039;m using:</p><div class="codebox"><pre><code>      &lt;% f.fields_for :things do |things_form| %&gt;
      &lt;tr class=&quot;&lt;%= cycle(&quot;odd&quot;,&quot;even&quot;) %&gt;&quot;&gt;
        &lt;td&gt;
          &lt;%= things_form.text_field :description, :size =&gt; 30 %&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;%= things_form.text_field :value %&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;% end %&gt;</code></pre></div><p>Now, because fields_for automatically spits out the hidden input field for the ID, this is the resultant HTML (with the surrounding table included):</p><div class="codebox"><pre><code>&lt;table cellspacing=&quot;1&quot; cellpadding=&quot;2&quot;&gt;
    &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Description&lt;/th&gt;
      &lt;th&gt;Current value&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;

    &lt;tbody&gt;

      &lt;input id=&quot;application_thing_attributes_0_id&quot; name=&quot;application[thing_attributes][0][id]&quot; type=&quot;hidden&quot; value=&quot;36912&quot;&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;input id=&quot;application_thing_attributes_0_description&quot; maxlength=&quot;255&quot; name=&quot;application[thing_attributes][0][description]&quot; value=&quot;Blah&quot;&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;input id=&quot;application_thing_attributes_0_asset_value&quot; maxlength=&quot;15&quot; name=&quot;application[thing_attributes][0][asset_value]&quot; size=&quot;10&quot; type=&quot;text&quot; value=&quot;400&quot;&gt;
        &lt;/td&gt;
      &lt;/tr&gt;

      &lt;input id=&quot;application_thing_attributes_1_id&quot; name=&quot;application[thing_attributes][1][id]&quot; type=&quot;hidden&quot; value=&quot;36913&quot;&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;input id=&quot;application_thing_attributes_1_description&quot; maxlength=&quot;255&quot; name=&quot;application[thing_attributes][1][description]&quot; size=&quot;30&quot; type=&quot;text&quot; value=&quot;&quot;&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;input id=&quot;application_thing_attributes_1_asset_value&quot; maxlength=&quot;15&quot; name=&quot;application[thing_attributes][1][asset_value]&quot; size=&quot;10&quot; type=&quot;text&quot; value=&quot;0&quot;&gt;
        &lt;/td&gt;
      &lt;/tr&gt;

    &lt;/tbody&gt;
&lt;/table&gt;</code></pre></div><p>If you look closely, the HTML is invalid because there is an &lt;input&gt; element nested underneath &lt;tbody&gt;.</p><p>How can I explicitly set where the ID field is rendered so that the fields_for loop will render valid HTML?</p>]]></description>
			<author><![CDATA[dummy@example.com (Dazza)]]></author>
			<pubDate>Tue, 22 Jun 2010 07:44:50 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=126838#p126838</guid>
		</item>
	</channel>
</rss>
