This project is archived and is in readonly mode.

#3426 new
Elad Meidar

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 invoking 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 an attr_accessor for 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_for options, 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 (apparently 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

  • Matt Jones

    Matt Jones October 26th, 2009 @ 02:49 AM

    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!

  • Elad Meidar

    Elad Meidar October 26th, 2009 @ 02:57 AM

    Well, Pratik and i talked and he suggested a totally different way to solve this issue, fields_for_with_index.

    As far as i am concerned, this patch is invalid :), i'll format a new one (better formatted too e.g) in the next couple of days.

  • Elad Meidar

    Elad Meidar October 26th, 2009 @ 05:08 AM

    Ok, this patch makes more sense.

    I've added FormHelper#fields_for_with_index and some tests (duplicates of some fields_for tests).

    Going through the entire fields_for cycle, 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_for to match the fields_for_with_index need 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"

  • Giulio Turetta

    Giulio Turetta November 6th, 2009 @ 05:00 PM

    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!

    so....


    uahhhh! yes! we can! :P

  • Chris Johnson

    Chris Johnson September 4th, 2010 @ 01:49 AM

    • Importance changed from “” to “”

    Seems like quite useful functionality; I need it right now.
    +1

  • Jarl Friis

    Jarl Friis October 20th, 2010 @ 10:54 AM

    • Tag changed from child_index, fields_for, form, form_helper to child_index, fields_for, form, formbuilder, form_helper

    +1 for the feature.

    I haven't look at the implentation/patch

  • Sakchai Siripanyawuth
  • Nick Hoffman

    Nick Hoffman March 13th, 2011 @ 09:04 PM

    +1 for this. It'll make it much easier to add new nested objects dynamically.

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>

Pages