method

label

Importance_2
Ruby on Rails latest stable (v2.3.4) - 1 note - Class: ActionView::Helpers::FormHelper
label(object_name, method, text = nil, options = {}) public

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, 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>

  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>
Show source
Register or log in to add new notes.
April 8, 2009 - (>= v2.2.1)
0 thanks

Translations of label method

The label method won’t use your translated attribute names - which seems like big disadvantage of this method.

For a quick workaround, try using this in a helper:

  def label(object_name, method, text = nil, options = {})
    text ||= object_name.classify.constantize.human_attribute_name(method.to_s)
    ActionView::Helpers::InstanceTag.new(object_name, method, self,
      options.delete(:object)).to_label_tag(text, options)
  end

I didn’t properly test this, but it seems to work.