From 7fadb3f261fc7cc753d303d0fbe2cbf019385f99 Mon Sep 17 00:00:00 2001 From: Will Read Date: Wed, 7 Oct 2009 18:47:26 -0700 Subject: Allow explicit placement of hidden id element for nested models. [#3259 state:resolved] Signed-off-by: Eloy Duran --- actionpack/lib/action_view/helpers/form_helper.rb | 13 ++++- actionpack/test/template/form_helper_test.rb | 66 +++++++++++++++++++---- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index c46b39fc23..d0c66eda60 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -963,7 +963,7 @@ module ActionView end end - (field_helpers - %w(label check_box radio_button fields_for)).each do |selector| + (field_helpers - %w(label check_box radio_button fields_for hidden_field)).each do |selector| src = <<-end_src def #{selector}(method, options = {}) # def text_field(method, options = {}) @template.send( # @template.send( @@ -1022,6 +1022,11 @@ module ActionView def radio_button(method, tag_value, options = {}) @template.radio_button(@object_name, method, tag_value, objectify_options(options)) end + + def hidden_field(method, options = {}) + @emitted_hidden_id = true if method == :id + @template.hidden_field(@object_name, method, objectify_options(options)) + end def error_message_on(method, *args) @template.error_message_on(@object, method, *args) @@ -1035,6 +1040,10 @@ module ActionView @template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit")) end + def emitted_hidden_id? + @emitted_hidden_id + 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_id? end end end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 04c635e770..44734abb18 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -611,6 +611,26 @@ class FormHelperTest < ActionView::TestCase end end + 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_field(:id) + concat af.text_field(:name) + end + end + expected = '
' + '' + '' + @@ -632,6 +652,30 @@ class FormHelperTest < ActionView::TestCase end end + 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_field(:id) + concat cf.text_field(:name) + end + end + end + expected = '
' + '' + '' + @@ -678,8 +722,8 @@ class FormHelperTest < ActionView::TestCase expected = '' + '' + - '' + '' + + '' + '' + '
' @@ -713,10 +757,10 @@ class FormHelperTest < ActionView::TestCase expected = '
' + '' + - '' + '' + - '' + + '' + '' + + '' + '
' assert_dom_equal expected, output_buffer @@ -736,8 +780,8 @@ class FormHelperTest < ActionView::TestCase expected = '
' + '' + - '' + '' + + '' + '' + '
' @@ -755,8 +799,8 @@ class FormHelperTest < ActionView::TestCase end expected = '
' + - '' + '' + + '' + '
' assert_dom_equal expected, output_buffer @@ -790,18 +834,18 @@ class FormHelperTest < ActionView::TestCase end expected = '
' + - '' + '' + - '' + '' + - '' + + '' + + '' + '' + - '' + '' + - '' + + '' + + '' + '' + - '' + '' + + '' + + '' + '
' assert_dom_equal expected, output_buffer -- cgit v1.2.3