ActionView::Helpers::FormHelper
def label(object_name, method, content_or_options = nil, options = nil, &block)

Description

Creates a <label> for a model attribute.

Arguments

Changing a value updates the ERB and HTML.

object_name:user
method:plan
= nil
The string used as the content of the <label>. Ignored when a block is given. A hash is treated as options (so the fourth argument, options, can be omitted). By default, the content of the <label> is the first of the following that exists:
  1. The helpers.label.user.plan translation
  2. If there is an object (@user), the activerecord.attributes.user.plan translation (what human_attribute_name returns)
  3. The attribute name passed through humanize (Plan)
options= nil
"plan_select"
Replaces the generated for attribute. The :value option, if given, is not appended to it. nil omits the for attribute.
"premium"
Used in two ways. Does not add the value attribute.
  • The for attribute: appended to it, with spaces and . replaced by _, punctuation other than - removed, and the result downcased.
  • The translation of the content: Rails also tries the key with this string after the attribute name (such as helpers.label.user.plan.premium). With an object, the second and third steps for the content (see content_or_options) also use this string, so without a translation the content is this string humanized (Premium); the attribute's translation (activerecord.attributes.user.plan) is not used.
Common options

Rails handles these options the same way in every helper. Options the HTML spec does not allow on the <label>, and ones this site considers rarely used, are not listed here.

Shared by all helpers

{active: true, hidden: false}
A hash gives the keys whose values are not false or nil, joined with spaces. An array gives its elements joined with spaces, after removing nil, false and empty strings (including whitespace-only strings).
{controller: "tags", tags_suggestions_value: ["ruby", "rails"]}
A hash adds data-* attributes, with _ in the keys converted to -. Array and hash values are converted to JSON strings.
Any other options become HTML attributes of the <label>; those with a nil value are left out.
&block= nil

ERB

Changes from the previous output: Added Removed Changed


          

HTML


          

Preview

Interacting with the preview updates the form submission.

Form submission

Form data

namevalue
Request body

Content-Type:


              

params