instance method
label
Ruby on Rails 3.1.12
Since v2.2.3Signature
label(object_name, method, content_or_options = nil, options = nil, &block)
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 a translation is found in the current I18n locale (through helpers.label.<modelname>.<attribute>) or 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, except for the :value option, which is designed to target labels for radio_button tags (where the value is used in the ID of the input tag).
Examples
label(:post, :title)
# => <label for="post_title">Title</label>
You can localize your labels based on model and attribute names.
For example you can define the following in your locale (e.g. en.yml)
helpers:
label:
post:
body: "Write your entire text here"
Which then will result in
label(:post, :body)
# => <label for="post_body">Write your entire text here</label>
Localization can also be based purely on the translation of the attribute-name (if you are using ActiveRecord):
activerecord:
attributes:
post:
cost: "Total cost"
label(:post, :cost)
# => <label for="post_cost">Total cost</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>
label(:post, :privacy, "Public Post", :value => "public")
# => <label for="post_privacy_public">Public Post</label>
label(:post, :terms) do
'Accept <a href="/terms">Terms</a>.'.html_safe
end
Parameters
-
object_namereq -
methodreq -
content_or_optionsopt = nil -
optionsopt = nil -
blockblock
Source
# File actionpack/lib/action_view/helpers/form_helper.rb, line 642
def label(object_name, method, content_or_options = nil, options = nil, &block)
content_is_options = content_or_options.is_a?(Hash)
if content_is_options || block_given?
options = content_or_options if content_is_options
text = nil
else
text = content_or_options
end
options ||= {}
InstanceTag.new(object_name, method, self, options.delete(:object)).to_label_tag(text, options, &block)
end
Defined in actionpack/lib/action_view/helpers/form_helper.rb line 642
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::FormHelper