diff options
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 9 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_tag_helper.rb | 5 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 6 |
3 files changed, 20 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 8aa0b42fe1..8d89f74c3b 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -223,6 +223,11 @@ module ActionView def radio_button(object_name, method, tag_value, options = {}) InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_radio_button_tag(tag_value, options) end + + + def label(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_label_tag(options) + end end class InstanceTag #:nodoc: @@ -328,6 +333,10 @@ module ActionView tag_text << ">True</option></select>" end + def to_label_tag(options = {}) + label_tag(options.delete(:text) || value(object).humanize, options.reverse_merge(:for => tag_id)) + end + def to_content_tag(tag_name, options = {}) content_tag(tag_name, value(object), options) end diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index e2e796e7c8..661034e5ca 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -153,6 +153,11 @@ module ActionView tag :input, html_options end + # Creates a label tag. + def label_tag(text, options = {}) + content_tag :label, text, options + end + # Creates a submit button with the text <tt>value</tt> as the caption. If options contains a pair with the key of "disable_with", # then the value will be used to rename a disabled version of the submit button. def submit_tag(value = "Save changes", options = {}) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index a7ceb6b30f..f7d51b2f2d 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -163,6 +163,12 @@ class FormHelperTest < Test::Unit::TestCase ) end + def test_label + assert_dom_equal('<label for="post_body">Body</label>', label("post", "body")) + assert_dom_equal('<label for="post_body">Super body</label>', label("post", "body", :text => "Super body")) + assert_dom_equal('<label for="post_body" class="strong">Super body</label>', label("post", "body", :text => "Super body", :class => "strong")) + end + def test_explicit_name assert_dom_equal( '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess") |