From 7362422017b3edc86c86800549a70e5d69d7f8a4 Mon Sep 17 00:00:00 2001 From: Will Read Date: Wed, 7 Oct 2009 18:47:26 -0700 Subject: [PATCH] Allow explicit placement of hidden id element for nested models. --- 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 32b9c4a..20f6cc1 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_field = 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) @@ -1034,6 +1039,10 @@ module ActionView def submit(value = "Save changes", options = {}) @template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit")) end + + def emitted_hidden_field? + @emitted_hidden_field + end private def objectify_options(options) @@ -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_field? end end end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 6a08c99..8102d85 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -613,6 +613,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_field(:id) + concat af.text_field(:name) + end + end + + expected = '
' + + '' + '' + '' + '
' @@ -634,6 +654,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_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 -- 1.6.3.3