This project is archived and is in readonly mode.
Accept arrays for nested attributes fields_for
Reported by Andrew France | May 15th, 2009 @ 01:20 AM | in 2.3.6
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.
Comments and changes to this ticket
-
Eloy Duran June 9th, 2009 @ 12:22 PM
- Assigned user set to Eloy Duran
-
Eloy Duran July 12th, 2009 @ 12:43 PM
- 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!
-
Eloy Duran July 12th, 2009 @ 12:43 PM
PS: Please do a sanity check if my refactoring of the if-else branches don't break your intention in some way :)
-
Andrew France July 13th, 2009 @ 02:43 PM
That's much neater and as far as I can tell the logic is identical, thanks Eloy!
-
Jeremy Kemper September 11th, 2009 @ 11:04 PM
- Milestone changed from 2.3.4 to 2.3.6
[milestone:id#50064 bulk edit command]
-
Repository September 13th, 2009 @ 02:33 AM
- 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 state:resolved] Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
http://github.com/rails/rails/commit/55bc0c76f8b9d19402ce51623d027f... -
Repository September 13th, 2009 @ 02:33 AM
(from [1b78e9bba3bd39c4669ff6c640b7df069185c22c]) Allow fields_for on a nested_attributes association to accept an explicit collection to be used. [#2648 state:resolved] Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
http://github.com/rails/rails/commit/1b78e9bba3bd39c4669ff6c640b7df...
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>
People watching this ticket
Attachments
Referenced by
- 2648 Accept arrays for nested attributes fields_for (from [55bc0c76f8b9d19402ce51623d027feb03dced41]) Allow f...
- 2648 Accept arrays for nested attributes fields_for (from [1b78e9bba3bd39c4669ff6c640b7df069185c22c]) Allow f...
- 2365 add :new_if_blank option to fields_for for nested associations Association array determination is a problem I looked at ...