Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

current child index on fields_for nested attributes

#3426

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.

Reported by Elad Meidar · October 25th, 2009 @ 11:47 PM

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Matt Jones
    Matt Jones

    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!

    October 26th, 2009 @ 02:49 AM

  2. Elad Meidar
    Elad Meidar

    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.

    October 26th, 2009 @ 02:57 AM

  3. Elad Meidar
    Elad Meidar

    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"

    October 26th, 2009 @ 05:08 AM

  4. Giulio Turetta
    Giulio Turetta

    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

    November 6th, 2009 @ 05:00 PM

  5. Chris Johnson
    Chris Johnson
    • Importance changed from to

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

    September 4th, 2010 @ 01:49 AM

  6. Jarl Friis
    Jarl Friis
    • 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

    October 20th, 2010 @ 10:54 AM

  7. Sakchai Siripanyawuth
  8. Nick Hoffman
    Nick Hoffman

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

    March 13th, 2011 @ 09:04 PM