This project is archived and is in readonly mode.
current child index on fields_for nested attributes
Reported by Elad Meidar | October 25th, 2009 @ 11:47 PM
I tried to find a way to access the current child index while
fields_for on a collection, i needed that in
order to provide meaningful CSS selectors for each and every item
and i wasn't able to find a way to access it.
The patch i added (for 2-3-stable) is basically an addition of
ActionView::Helpers::FormBuilder that holds the
current child index and a method named
current_child_index that returns the current index
based on those guidelines:
- If there is only one item (a single object or an association with 1 item), the index returned will be always 0
- if there's a specific :index directive on the
fields_foroptions, return it.
- otherwise, return the actual index (existing methodology to determine current index is kept).
In order to preserve the value between FormBuilder's instances
FormHelper#fields_for is creating one for
each iteration), I've added a conditional statement that carries
the value from the initial builder (the one that is calling the
FormHelper one) to the next iteration.
Confused? so do i.
Comments and changes to this ticket
Patch looks useful, but a couple formatting notes:
It's hard to tell what this patch does, as it's got several commits in it. You'll want to format it as a single commit for inclusion in Rails.
Don't reformat existing code; it makes the changelog noisy and doesn't serve a purpose. I'm specifically referencing the blank lines after 'def foo_bar(x,y,z)'.
Similarly, blank lines shouldn't have spaces - especially not lines that were really blank before (just \n) and are now "\n ". It's always good to turn on visible newlines (or equivalent in your editor) and make sure that there's not trailing whitespace.
Thanks for contributing!
Ok, this patch makes more sense.
FormHelper#fields_for_with_indexand some tests (duplicates of some fields_for tests).
Going through the entire
fields_forcycle, i found that it was really integrated with the entire process of rendering a form, more specifically
fields_for_with_index_nested_model. i chose to duplicate the entire method chain of
fields_forto match the
fields_for_with_indexneed in a second block parameter ( |builder, index| ) mainly because i didn't find a better way to patch the existing code to consider that, and @Matt's 2nd note "don't reformat existing code"
this patch is useful!
in the mean time (on 2.3.2) I use a wrong/bad/ridiculous solution :)
<% f.fields_for :emails do |email_fields| %>
<%= email_fields.text_field :address %>
<% end %>
email_fields.object_name.gsub(/[^0-9]+/,'') => actual fields_for index!
uahhhh! yes! we can! :P
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>