This Entangled Bank: editing ActiveRecord collection with related drop-downs using related_select_formsmay contain traces of knowledgetag:entangledbank.co.uk,2005:TypoTypo2007-09-11T11:07:28+01:00Edurn:uuid:7fc8e690-e059-40cb-806e-d8b8caf4f2e92007-09-11T10:13:00+01:002007-09-11T11:07:28+01:00editing ActiveRecord collection with related drop-downs using related_select_forms<p>Another problem setting the <span class="caps">HTML</span> <code>name</code> attribute when editing a collection of ActiveRecord objects in a Rails application. This time I was using the <code>related_select_forms</code> plugin to build related drop-down (select) lists. The plugin which creates all the necessary javascript to dynamically reload the second list according to the selection in the first list is kindly provided by Dimitrij Denissenko and available <a href="http://dvisionfactory.googlecode.com/svn/rails/plugins/related_select_forms/">here</a>.</p><p>Another problem setting the <span class="caps">HTML</span> <code>name</code> attribute when editing a collection of ActiveRecord objects in a Rails application. This time I was using the <code>related_select_forms</code> plugin to build related drop-down (select) lists. The plugin which creates all the necessary javascript to dynamically reload the second list according to the selection in the first list is kindly provided by Dimitrij Denissenko and available <a href="http://dvisionfactory.googlecode.com/svn/rails/plugins/related_select_forms/">here</a>.</p><p>After a bit of research I found that the <code>name</code> argument can be arbitrarily defined in the <code>html_options</code> hash passed as the final argument to the select list helper (see the docs <a href="http://www.railshelp.com/ActionView::Helpers::FormOptionsHelper">here</a>).</p>
<p>First I had to build up the <code>name</code> strings for the html in the format: </p>
<pre><code>product[1424][field_name]</code></pre>
<div class="typocode"><pre><code class="typocode_default ">theme_name = "product[" + @product.id.to_s + "][theme_id]"
sub_theme_name = "product[" + @product.id.to_s + "][sub_theme_id]"</code></pre></div>
<strong>first select list</strong>
<div class="typocode"><pre><code class="typocode_default "><%= select(:theme, :id, theme_list, {},
{ :name => theme_name } ) %></code></pre></div>
<p><strong>second select list</strong>
<div class="typocode"><pre><code class="typocode_default ">
<%= related_collection_select(
:sub_theme, :id, [:theme, :id], SubTheme.live_list,
:id, :name, :theme_id,
{},
{ :name => sub_theme_name } ) %></code></pre></div></p>
<p>NB: there is a slight gotcha here with the hashes. The first (empty) hash is the <code>options{} </code>hash used only for <code>{:include_blank => true}</code>, the second hash is used to set whatever <span class="caps">HTML</span> attributes you need, eg: <code>{ :selected => @product.sub_theme_id, :multiple => true, :name => sub_name }</code>. You <strong>must</strong> include the first hash, empty if you don’t want to use <code>:include_blank => true</code>.</p>