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.

Accept arrays for nested attributes fields_for

#2648

It would be nice to supply an array to fields_for when working with nested attributes, like so:

# In controller:
@selected_books = @author.books.all(:conditions => {:selected => true})

View:




form_for :author do |f| f.text_field :name f.fields_for(:books, @selected_books) do |f2|
f2.text_field :name



end end

This is instead of iterating over the array as currently shown in the docs:

form_for :author do |f|
  ...
  @selected_books.each do |book|

f.fields_for(:books, book) do |f2|
  ...
end



end end

Supplying an array seems more elegant to me and stops the parent object association being called for every iteration.

The supplied patch passes the existing tests and adds a couple more that are appropriate. Although the nested if statements are a bit messy so any help making it cleaner would be welcome.

Reported by Andrew France · May 15th, 2009 @ 01:20 AM

State: resolved
Milestone: 2.3.6
Assigned to: Eloy Duran Eloy Duran
Importance: none

Activity

  1. frap73
  2. Eloy Duran
    Eloy Duran
    • Assigned user set to Eloy Duran

    June 9th, 2009 @ 12:22 PM

  3. Eloy Duran
    Eloy Duran
    • State changed from new to verified
    • Milestone changed from 2.x to 2.3.4

    I have verified and applied this patch on my branch of 2-3-stable, which Micheal will look at and merge in before 2.3.4.

    http://github.com/alloy/rails/commit/c2e01228fc3fa2b4a238a68a98be49...

    Thanks!

    July 12th, 2009 @ 12:43 PM

  4. Eloy Duran
    Eloy Duran

    PS: Please do a sanity check if my refactoring of the if-else branches don't break your intention in some way :)

    July 12th, 2009 @ 12:43 PM

  5. Andrew France
    Andrew France

    That's much neater and as far as I can tell the logic is identical, thanks Eloy!

    July 13th, 2009 @ 02:43 PM

  6. Eloy Duran
    Eloy Duran

    Thanks for verifying :)

    July 13th, 2009 @ 05:26 PM

  7. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.4 to 2.3.6

    [milestone:id#50064 bulk edit command]

    September 11th, 2009 @ 11:04 PM

  8. Repository
    Repository
    • State changed from verified to resolved

    (from [55bc0c76f8b9d19402ce51623d027feb03dced41]) Allow fields_for on a nested_attributes association to accept an explicit collection to be used. [#2648 Accept arrays for nested attributes fields_for state:resolved] Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
    http://github.com/rails/rails/commit/55bc0c76f8b9d19402ce51623d027f...

    September 13th, 2009 @ 02:33 AM

  9. Repository
    Repository

    (from [1b78e9bba3bd39c4669ff6c640b7df069185c22c]) Allow fields_for on a nested_attributes association to accept an explicit collection to be used. [#2648 Accept arrays for nested attributes fields_for state:resolved] Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
    http://github.com/rails/rails/commit/1b78e9bba3bd39c4669ff6c640b7df...

    September 13th, 2009 @ 02:33 AM