From ca1dc10276b46e0572ffd25d24fd2afceb8dc4ce Mon Sep 17 00:00:00 2001 From: pivotal Date: Fri, 25 Sep 2009 13:19:48 -0700 Subject: [PATCH] Allow explicit placement of hidden elements for nested models. --- actionpack/lib/action_view/helpers/form_helper.rb | 11 +++- actionpack/test/template/form_helper_test.rb | 66 +++++++++++++++++---- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 32b9c4a..d4d798f 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -1035,6 +1035,15 @@ module ActionView @template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit")) end + def hidden_fields + @emitted_hidden_fields = true + self.hidden_field(:id) + end + + def emitted_hidden_fields? + @emitted_hidden_fields + end + private def objectify_options(options) @default_options.merge(options.merge(:object => @object)) @@ -1069,8 +1078,8 @@ module ActionView @template.fields_for(name, object, *args, &block) else @template.fields_for(name, object, *args) do |builder| - @template.concat builder.hidden_field(:id) block.call(builder) + @template.concat builder.hidden_field(:id) unless builder.emitted_hidden_fields? end end end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index be15b06..f2009bb 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -711,6 +711,26 @@ class FormHelperTest < ActionView::TestCase expected = '
' + '' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + end + + def test_nested_fields_for_with_existing_records_on_a_nested_attributes_one_to_one_association_with_explicit_hidden_field_placement + @post.author = Author.new(321) + + form_for(:post, @post) do |f| + concat f.text_field(:title) + f.fields_for(:author) do |af| + concat af.hidden_fields + concat af.text_field(:name) + end + end + + expected = '
' + + '' + '' + '' + '
' @@ -732,6 +752,30 @@ class FormHelperTest < ActionView::TestCase expected = '
' + '' + + '' + + '' + + '' + + '' + + '
' + + assert_dom_equal expected, output_buffer + end + + def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association_with_explicit_hidden_field_placement + @post.comments = Array.new(2) { |id| Comment.new(id + 1) } + + form_for(:post, @post) do |f| + concat f.text_field(:title) + @post.comments.each do |comment| + f.fields_for(:comments, comment) do |cf| + concat cf.hidden_fields + concat cf.text_field(:name) + end + end + end + + expected = '
' + + '' + '' + '' + '' + @@ -776,8 +820,8 @@ class FormHelperTest < ActionView::TestCase expected = '' + '' + - '' + '' + + '' + '' + '
' @@ -811,10 +855,10 @@ class FormHelperTest < ActionView::TestCase expected = '
' + '' + - '' + '' + - '' + + '' + '' + + '' + '
' assert_dom_equal expected, output_buffer @@ -834,8 +878,8 @@ class FormHelperTest < ActionView::TestCase expected = '
' + '' + - '' + '' + + '' + '' + '
' @@ -853,8 +897,8 @@ class FormHelperTest < ActionView::TestCase end expected = '
' + - '' + '' + + '' + '
' assert_dom_equal expected, output_buffer @@ -888,18 +932,18 @@ class FormHelperTest < ActionView::TestCase end expected = '
' + - '' + '' + - '' + '' + - '' + + '' + + '' + '' + - '' + '' + - '' + + '' + + '' + '' + - '' + '' + + '' + + '' + '
' assert_dom_equal expected, output_buffer -- 1.6.3.3