diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-04-06 19:25:03 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-04-06 19:25:03 -0300 |
commit | 693b333920fc2f5b74f157d46d531cd97b692006 (patch) | |
tree | bba09ddab8113aadbf28c4168b2b0747139652cd /actionview | |
parent | d5f80655f9d285cf569ec28e741bbc4bd16e6599 (diff) | |
parent | a813fa09a86ec522b43f16d137bd99e0a43985f5 (diff) | |
download | rails-693b333920fc2f5b74f157d46d531cd97b692006.tar.gz rails-693b333920fc2f5b74f157d46d531cd97b692006.tar.bz2 rails-693b333920fc2f5b74f157d46d531cd97b692006.zip |
Merge pull request #19661 from Azdaroth/fields-for-child-index-lambda
Accept lambda as child_index option in #fields_for method
Diffstat (limited to 'actionview')
-rw-r--r-- | actionview/CHANGELOG.md | 4 | ||||
-rw-r--r-- | actionview/lib/action_view/helpers/form_helper.rb | 6 | ||||
-rw-r--r-- | actionview/test/template/form_helper_test.rb | 17 |
3 files changed, 26 insertions, 1 deletions
diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 9d40bd6d5d..80aacf7234 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,7 @@ +* Accept lambda as `child_index` option in `fields_for` method. + + *Karol Galanciak* + * `translate` allows `default: [[]]` again for a default value of `[]`. Fixes #19640. diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index 891cc53765..ece117b547 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -1928,7 +1928,11 @@ module ActionView explicit_child_index = options[:child_index] output = ActiveSupport::SafeBuffer.new association.each do |child| - options[:child_index] = nested_child_index(name) unless explicit_child_index + if explicit_child_index + options[:child_index] = explicit_child_index.call if explicit_child_index.respond_to?(:call) + else + options[:child_index] = nested_child_index(name) + end output << fields_for_nested_model("#{name}[#{options[:child_index]}]", child, options, block) end output diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb index 4e336bea63..5c55b154d3 100644 --- a/actionview/test/template/form_helper_test.rb +++ b/actionview/test/template/form_helper_test.rb @@ -2878,6 +2878,23 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_nested_fields_for_with_child_index_as_lambda_option_override_on_a_nested_attributes_collection_association + @post.comments = [] + + form_for(@post) do |f| + concat f.fields_for(:comments, Comment.new(321), child_index: -> { 'abc' } ) { |cf| + concat cf.text_field(:name) + } + end + + expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', method: 'patch') do + '<input id="post_comments_attributes_abc_name" name="post[comments_attributes][abc][name]" type="text" value="comment #321" />' + + '<input id="post_comments_attributes_abc_id" name="post[comments_attributes][abc][id]" type="hidden" value="321" />' + end + + assert_dom_equal expected, output_buffer + end + class FakeAssociationProxy def to_ary [1, 2, 3] |