Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use the select_tag helper in a nested form?
    primarykey
    data
    text
    <p>UPDATE: SOLUTION AT BOTTOM</p> <p>I'm hoping someone else has spent some time ironing out this same issue... I'm building an address book within an rails application, and I have an Person model and an Email model. Person has_many Emails, and Person accepts_nested_attributes_for :emails. </p> <p>This all works perfectly -- I have a nested form with which I can add and remove emails and their attributes in my form (using a method similar to Railscasts 196-7 (<a href="http://railscasts.com/episodes/196-nested-model-form-part-1" rel="nofollow noreferrer">here</a>)). Submitting the form creates the Person, as well as the Emails, with all the correct associations. </p> <p>The problem is, as it stands, I'm unable to find a way to use a select tag on the nested email model for the user to select a KIND (e.g. work, home, other) for that email. So, editing the Person model as I have it now, there is no easy way to select the attribute that is stored in the rails model.</p> <p>Here's what I have now:</p> <pre><code>&lt;%= form_for(@source) do |f| %&gt; &lt;% f.fields_for :emails do |builder| %&gt; . . . &lt;%= builder.text_field :address, :size =&gt; 15 %&gt; &lt;%= render :partial =&gt; 'email_select_1' %&gt;0&lt;%= render :partial =&gt; 'email_select_2' %&gt;0&lt;%= render :partial =&gt; 'email_select_3' %&gt; </code></pre> <p>Note: the above is a hacky way of allowing me to set the ID later in javascript using the same code fragments. The partials spit out a hard-coded select and options, in this case '0' but in javascript I can change it to a unique:</p> <pre><code>&lt;select id="person_emails_attributes_0_kind" name="person[emails_attributes][0][kind]"&gt;&lt;option value="work"&gt;work&lt;/option&gt;&lt;option value="home"&gt;home&lt;/option&gt;&lt;option value="other"&gt;other&lt;/option&gt;&lt;/select&gt; </code></pre> <p>and then the rest looks like this (abbrev):</p> <pre><code>. . . &lt;% end %&gt; . . . &lt;!-- more form stuff, actions, etc... --&gt; . . . &lt;% end %&gt; </code></pre> <p>Is there a way to use the select_tag helper for this, and replace my hacky-hard-coded select? If not, any suggestions for writing my own helper method?</p> <p>Thanks in advance!</p> <p>UPDATE: Thanks to Geoff for the solution. For posterity, the big thing is to use select, not select_tag. Then you can just do something like this:</p> <pre><code>&lt;%= builder.select :kind, [['work', 'work'], ['home', 'home'], ['other', 'other']]%&gt; </code></pre>
    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.
    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