Skip to content

Commit 11614bd

Browse files
carlosantoniodasilvaspastorino
authored andcommitted
Fix label form helper to use I18n and html options, without the need of 'nil' text param:
Before: f.label :title, nil, :class => 'title' After : f.label :title, :class => 'title' [rails#5267 state:committed] Signed-off-by: Santiago Pastorino <[email protected]>
1 parent fb0bd8c commit 11614bd

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

actionpack/lib/action_view/helpers/form_helper.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ module FormHelper
215215
# ...
216216
# <% end %>
217217
#
218-
# If your resource has associations defined, for example, you want to add comments
218+
# If your resource has associations defined, for example, you want to add comments
219219
# to the post given that the routes are set correctly:
220220
#
221221
# <%= form_for([@document, @comment]) do |f| %>
@@ -583,8 +583,9 @@ def fields_for(record_or_name_or_array, *args, &block)
583583
# 'Accept <a href="/terms">Terms</a>.'
584584
# end
585585
def label(object_name, method, content_or_options = nil, options = nil, &block)
586-
if block_given?
587-
options = content_or_options if content_or_options.is_a?(Hash)
586+
content_is_options = content_or_options.is_a?(Hash)
587+
if content_is_options || block_given?
588+
options = content_or_options if content_is_options
588589
text = nil
589590
else
590591
text = content_or_options

actionpack/test/template/form_helper_test.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ def test_label_with_locales_symbols
120120
I18n.locale = old_locale
121121
end
122122

123+
def test_label_with_locales_and_options
124+
old_locale, I18n.locale = I18n.locale, :label
125+
assert_dom_equal('<label for="post_body" class="post_body">Write entire text here</label>', label(:post, :body, :class => 'post_body'))
126+
ensure
127+
I18n.locale = old_locale
128+
end
129+
123130
def test_label_with_for_attribute_as_symbol
124131
assert_dom_equal('<label for="my_for">Title</label>', label(:post, :title, nil, :for => "my_for"))
125132
end
@@ -620,15 +627,15 @@ def test_form_for
620627

621628
def test_form_for_with_symbol_object_name
622629
form_for(@post, :as => "other_name", :html => { :id => 'create-post' }) do |f|
623-
concat f.label(:title)
630+
concat f.label(:title, :class => 'post_title')
624631
concat f.text_field(:title)
625632
concat f.text_area(:body)
626633
concat f.check_box(:secret)
627634
concat f.submit('Create post')
628635
end
629636

630637
expected = whole_form("/posts/123", "create-post", "other_name_edit", :method => "put") do
631-
"<label for='other_name_title'>Title</label>" +
638+
"<label for='other_name_title' class='post_title'>Title</label>" +
632639
"<input name='other_name[title]' size='30' id='other_name_title' value='Hello World' type='text' />" +
633640
"<textarea name='other_name[body]' id='other_name_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
634641
"<input name='other_name[secret]' value='0' type='hidden' />" +

0 commit comments

Comments
 (0)