aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/form_tag_helper.rb
diff options
context:
space:
mode:
authorStephen Celis <stephen@stephencelis.com>2010-04-20 23:16:18 -0500
committerJosé Valim <jose.valim@gmail.com>2010-05-15 08:59:08 +0200
commit6e69b42b21d8e76c4d87b6fbc4222f55d3b11a06 (patch)
treef3c036e8d4cfe261984bdba8d14cae2612dc9fd0 /actionpack/lib/action_view/helpers/form_tag_helper.rb
parent35a114a8941cb22d29a536f1215a23a8cf7c4756 (diff)
downloadrails-6e69b42b21d8e76c4d87b6fbc4222f55d3b11a06.tar.gz
rails-6e69b42b21d8e76c4d87b6fbc4222f55d3b11a06.tar.bz2
rails-6e69b42b21d8e76c4d87b6fbc4222f55d3b11a06.zip
Let label helpers accept blocks.
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'actionpack/lib/action_view/helpers/form_tag_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/form_tag_helper.rb13
1 files changed, 10 insertions, 3 deletions
diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb
index b840f77fb5..9d15805d46 100644
--- a/actionpack/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -142,7 +142,7 @@ module ActionView
tag :input, { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
end
- # Creates a label field
+ # Creates a label element. Accepts a block.
#
# ==== Options
# * Creates standard HTML attributes for the tag.
@@ -156,8 +156,15 @@ module ActionView
#
# 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.to_s.humanize, { "for" => sanitize_to_id(name) }.update(options.stringify_keys)
+ def label_tag(name = nil, content_or_options_with_block = nil, options = nil, &block)
+ if block_given?
+ options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
+ end
+
+ options ||= {}
+ options.stringify_keys!
+ options["for"] = sanitize_to_id(name) unless name.blank? || options.has_key?("for")
+ content_tag :label, content_or_options_with_block || name.to_s.humanize, options, &block
end
# Creates a hidden form input field used to transmit data that would be lost due to HTTP's statelessness or