mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #21431 from ojab/master
Handle nested fields_for by adding indexes to record_name
This commit is contained in:
commit
5b64835601
2 changed files with 29 additions and 1 deletions
|
@ -1617,7 +1617,14 @@ module ActionView
|
|||
@auto_index
|
||||
end
|
||||
|
||||
record_name = index ? "#{object_name}[#{index}][#{record_name}]" : "#{object_name}[#{record_name}]"
|
||||
record_name = if index
|
||||
"#{object_name}[#{index}][#{record_name}]"
|
||||
elsif record_name.to_s.end_with?('[]')
|
||||
record_name = record_name.to_s.sub(/(.*)\[\]$/, "[\\1][#{record_object.id}]")
|
||||
"#{object_name}#{record_name}"
|
||||
else
|
||||
"#{object_name}[#{record_name}]"
|
||||
end
|
||||
fields_options[:child_index] = index
|
||||
|
||||
@template.fields_for(record_name, record_object, fields_options, &block)
|
||||
|
|
|
@ -2297,6 +2297,27 @@ class FormHelperTest < ActionView::TestCase
|
|||
assert_dom_equal expected, output_buffer
|
||||
end
|
||||
|
||||
def test_deep_nested_fields_for
|
||||
@comment.save
|
||||
form_for(:posts) do |f|
|
||||
f.fields_for('post[]', @post) do |f2|
|
||||
f2.text_field(:id)
|
||||
@post.comments.each do |comment|
|
||||
concat f2.fields_for('comment[]', comment) { |c|
|
||||
concat c.text_field(:name)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
expected = whole_form do
|
||||
"<input name='posts[post][0][comment][1][name]' type='text' id='posts_post_0_comment_1_name' value='comment #1' />"
|
||||
end
|
||||
|
||||
assert_dom_equal expected, output_buffer
|
||||
end
|
||||
|
||||
|
||||
def test_nested_fields_for_with_nested_collections
|
||||
form_for(@post, as: 'post[]') do |f|
|
||||
concat f.text_field(:title)
|
||||
|
|
Loading…
Reference in a new issue