diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-09-22 17:17:22 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-09-22 17:17:22 +0000 |
commit | a7764d8fd491fb49f50397aa79d305bdb93552c5 (patch) | |
tree | a8c47eddbdcd16175a6d3141849cb8ca94b7f0e2 /actionpack/lib | |
parent | a5af3f75af86ff0abd9964bfd69483d116c12b13 (diff) | |
download | rails-a7764d8fd491fb49f50397aa79d305bdb93552c5.tar.gz rails-a7764d8fd491fb49f50397aa79d305bdb93552c5.tar.bz2 rails-a7764d8fd491fb49f50397aa79d305bdb93552c5.zip |
Added FormHelper#label (closes #8641) [jcoglan]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7541 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 9c31fb6c90..9bfc3041af 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -219,6 +219,25 @@ module ActionView yield builder.new(object_name, object, self, options, block) end + # Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object + # assigned to the template (identified by +object+). The text of label will default to the attribute name unless you specify + # it explicitly. Additional options on the label tag can be passed as a hash with +options+. These options will be tagged + # onto the html as an HTML element attribute as in the example shown. + # + # ==== Examples + # label(:post, :title) + # #=> <label for="post_title">Title</label> + # + # label(:post, :title, "A short title") + # #=> <label for="post_title">A short title</label> + # + # label(:post, :title, "A short title", :class => "title_label") + # #=> <label for="post_title" class="title_label">A short title</label> + # + def label(object_name, method, text = nil, options = {}) + InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_label_tag(text, options) + end + # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a # hash with +options+. These options will be tagged onto the html as an HTML element attribute as in the example @@ -398,6 +417,14 @@ module ActionView end end + def to_label_tag(text = nil, options = {}) + name_and_id = options.dup + add_default_name_and_id(name_and_id) + options["for"] = name_and_id["id"] + content = (text.blank? ? nil : text.to_s) || method_name.humanize + content_tag("label", content, options) + end + def to_input_field_tag(field_type, options = {}) options = options.stringify_keys options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") @@ -574,7 +601,7 @@ module ActionView @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc end - (field_helpers - %w(check_box radio_button fields_for)).each do |selector| + (field_helpers - %w(label check_box radio_button fields_for)).each do |selector| src = <<-end_src def #{selector}(method, options = {}) @template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object)) @@ -588,6 +615,10 @@ module ActionView @template.fields_for(name, *args, &block) end + def label(method, text = nil, options = {}) + @template.label(@object_name, method, text, options.merge(:object => @object)) + end + def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") @template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value) end |