Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

2-0-stable: Add label_tag helper for generating elements. References #…

…10802 [DefV]

Merging [8685]


git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/2-0-stable@8830 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit c71de0330657509d98be901bed9c1e442bbde30d 1 parent 81991d6
@NZKoz NZKoz authored
View
2  actionpack/CHANGELOG
@@ -6,6 +6,8 @@
* Fix bug with setting Request#format= after the getter has cached the value. #10889 [cch1]
+* Add label_tag helper for generating elements. #10802 [DefV]
+
* TestSession supports indifferent access. #7372 [tamc, Arsen7, mhackett, julik, jean.helou]
* UrlWriter respects relative_url_root. #10748 [Cheah Chu Yeow]
View
18 actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -114,6 +114,24 @@ def text_field_tag(name, value = nil, options = {})
tag :input, { "type" => "text", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys)
end
+ # Creates a label field
+ #
+ # ==== Options
+ # * Creates standard HTML attributes for the tag.
+ #
+ # ==== Examples
+ # label_tag 'name'
+ # # => <label for="name">Name</label>
+ #
+ # label_tag 'name', 'Your name'
+ # # => <label for="name">Your Name</label>
+ #
+ # label_tag 'name', nil, :class => 'small_label'
+ # # => <label for="name" class="small_label">Name</label>
+ def label_tag(name, text = nil, options = {})
+ content_tag :label, text || name.humanize, { "for" => name }.update(options.stringify_keys)
+ end
+
# Creates a hidden form input field used to transmit data that would be lost due to HTTP's statelessness or
# data that should be hidden from the user.
#
View
18 actionpack/test/template/form_tag_helper_test.rb
@@ -188,6 +188,24 @@ def test_text_field_tag_with_multiple_options
assert_dom_equal expected, actual
end
+ def test_label_tag_without_text
+ actual = label_tag "title"
+ expected = %(<label for="title">Title</label>)
+ assert_dom_equal expected, actual
+ end
+
+ def test_label_tag_with_text
+ actual = label_tag "title", "My Title"
+ expected = %(<label for="title">My Title</label>)
+ assert_dom_equal expected, actual
+ end
+
+ def test_label_tag_class_string
+ actual = label_tag "title", "My Title", "class" => "small_label"
+ expected = %(<label for="title" class="small_label">My Title</label>)
+ assert_dom_equal expected, actual
+ end
+
def test_boolean_optios
assert_dom_equal %(<input checked="checked" disabled="disabled" id="admin" name="admin" readonly="readonly" type="checkbox" value="1" />), check_box_tag("admin", 1, true, 'disabled' => true, :readonly => "yes")
assert_dom_equal %(<input checked="checked" id="admin" name="admin" type="checkbox" value="1" />), check_box_tag("admin", 1, true, :disabled => false, :readonly => nil)
Please sign in to comment.
Something went wrong with that request. Please try again.