Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I access ActionView's fields_for index when iterating over many child records?
    primarykey
    data
    text
    <p>I have a Rails 2.3.10 app that uses fields_for to include fields on a has_many association's attributes. I'm using accepts_nested_attributes_for on the the parent model and its many children are being shown in the form using:</p> <pre><code>&lt;% form_for :map do |f| %&gt; ... &lt;% f.fields_for :markers do |marker_f| %&gt; &lt;%= marker_f.text_field :lat &lt;%= marker_f.text_field :long &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>The fields_for helper iterates over the child marker models and the text_field inputs are generated for each. These text fields have generated id values like: </p> <ul> <li><p>map_markers_attributes_0_lat and map_markers_attributes_0_long </p></li> <li><p>map_markers_attributes_1_lat and map_markers_attributes_1_long</p></li> <li><p>...</p></li> <li><p>map_markers_attributes_5_lat and map_markers_attributes_5_long</p></li> </ul> <p>The integer in the id is an index of the records array being iterated over. How do I access this index value for use elsewhere in my code?</p> <p>For example I want to use Javascript to access the id values on these inputs. </p> <pre><code>&lt;% form_for :map do |f| %&gt; ... &lt;% f.fields_for :markers do |marker_f| %&gt; &lt;%= marker_f.text_field :lat &lt;%= marker_f.text_field :long &lt;% content_for :head do %&gt; &lt;% javascript_tag do %&gt; $('&lt;%= generate_html_id(marker_f, :lat) %&gt;').observe_field('change'); $('&lt;%= generate_html_id(marker_f, :long) %&gt;').observe_field('change'); &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>I insert some javascript into the head of the html to observe the field for any changes. I need to know the id of the field and my generate_html_id method should be able to replicate the id of the field from the marker_f builder object. However I don't know how to get the index value coming from fields_for.</p> <p>It seems like there should be a fields_for_with_index method but there is not:</p> <pre><code>&lt;% form_for :map do |f| %&gt; ... &lt;% f.fields_for_with_index :markers do |marker_f, i| %&gt; &lt;%= marker_f.text_field :lat &lt;%= marker_f.text_field :long &lt;% content_for :head do %&gt; &lt;% javascript_tag do %&gt; $('&lt;%= generate_html_id(marker_f, :lat, i) %&gt;').observe_field('change'); $('&lt;%= generate_html_id(marker_f, :long, i) %&gt;').observe_field('change'); &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>BTW I could create custom id values for the field but would prefer to stick to the conventions.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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