Method: ActionView::Helpers::FormTagHelper#label_tag

Defined in:
actionview/lib/action_view/helpers/form_tag_helper.rb

#label_tag(name = nil, content_or_options = nil, options = nil, &block) ⇒ Object

Creates a label element. Accepts a block.

Options

  • Creates standard HTML attributes for the tag.

Examples

label_tag 'name'
# => <label for="name">Name</label>

label_tag 'name', 'Your name'
# => <label for="name">Your name</label>

label_tag 'name', nil, class: 'small_label'
# => <label for="name" class="small_label">Name</label>


281
282
283
284
285
286
287
288
289
290
# File 'actionview/lib/action_view/helpers/form_tag_helper.rb', line 281

def label_tag(name = nil, content_or_options = nil, options = nil, &block)
  if block_given? && content_or_options.is_a?(Hash)
    options = content_or_options = content_or_options.stringify_keys
  else
    options ||= {}
    options = options.stringify_keys
  end
  options["for"] = sanitize_to_id(name) unless name.blank? || options.has_key?("for")
   :label, content_or_options || name.to_s.humanize, options, &block
end