From 889bb4b786941fa2e73163e0b0de10175a999526 Mon Sep 17 00:00:00 2001 From: Alexey Vakhov Date: Sun, 1 Apr 2012 00:52:48 +0400 Subject: [PATCH 1/2] Block version of label should wrapped in field_with_errors in case of error --- actionpack/lib/action_view/helpers/tags/label.rb | 3 ++- actionpack/test/template/form_helper_test.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_view/helpers/tags/label.rb b/actionpack/lib/action_view/helpers/tags/label.rb index 1c8bf063ea1..ee97f8759b4 100644 --- a/actionpack/lib/action_view/helpers/tags/label.rb +++ b/actionpack/lib/action_view/helpers/tags/label.rb @@ -33,7 +33,8 @@ module ActionView options["for"] = name_and_id["id"] unless options.key?("for") if block_given? - @template_object.label_tag(name_and_id["id"], options, &block) + content = @template_object.capture(&block) + label_tag(name_and_id["id"], content, options) else content = if @content.blank? @object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1') diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 7b684822fca..f13296a1757 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -1080,6 +1080,20 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_for_label_error_wrapping_block_and_non_block_versions + form_for(@post) do |f| + concat f.label(:author_name, 'Name', :class => 'label') + concat f.label(:author_name, :class => 'label') { 'Name' } + end + + expected = whole_form('/posts/123', 'edit_post_123' , 'edit_post', 'patch') do + "
" + + "
" + end + + assert_dom_equal expected, output_buffer + end + def test_form_for_with_namespace form_for(@post, :namespace => 'namespace') do |f| concat f.text_field(:title) From 806f4d8af078884db3b68facb4c04560d449b9eb Mon Sep 17 00:00:00 2001 From: Alexey Vakhov Date: Sun, 1 Apr 2012 10:30:31 +0400 Subject: [PATCH 2/2] Small #label method refactoring, thanks @rafaelfranca --- actionpack/lib/action_view/helpers/tags/label.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_view/helpers/tags/label.rb b/actionpack/lib/action_view/helpers/tags/label.rb index ee97f8759b4..16135fcd5af 100644 --- a/actionpack/lib/action_view/helpers/tags/label.rb +++ b/actionpack/lib/action_view/helpers/tags/label.rb @@ -34,7 +34,6 @@ module ActionView if block_given? content = @template_object.capture(&block) - label_tag(name_and_id["id"], content, options) else content = if @content.blank? @object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1') @@ -56,9 +55,9 @@ module ActionView end content ||= @method_name.humanize - - label_tag(name_and_id["id"], content, options) end + + label_tag(name_and_id["id"], content, options) end end end