Method: ActionView::Helpers::FormTagHelper#radio_button_tag

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

#radio_button_tag(name, value, *args) ⇒ Object

:call-seq:

radio_button_tag(name, value, options = {})
radio_button_tag(name, value, checked, options = {})

Creates a radio button; use groups of radio buttons named the same to allow users to select from a group of options.

Options

  • :checked - If set to true, the radio button will be selected by default.

  • :disabled - If set to true, the user will not be able to use this input.

  • Any other key creates standard HTML options for the tag.

Examples

radio_button_tag 'favorite_color', 'maroon'
# => <input id="favorite_color_maroon" name="favorite_color" type="radio" value="maroon" />

radio_button_tag 'receive_updates', 'no', true
# => <input checked="checked" id="receive_updates_no" name="receive_updates" type="radio" value="no" />

radio_button_tag 'time_slot', "3:00 p.m.", false, disabled: true
# => <input disabled="disabled" id="time_slot_3:00_p.m." name="time_slot" type="radio" value="3:00 p.m." />

radio_button_tag 'color', "green", true, class: "color_input"
# => <input checked="checked" class="color_input" id="color_green" name="color" type="radio" value="green" />


496
497
498
499
500
501
502
503
504
505
# File 'actionview/lib/action_view/helpers/form_tag_helper.rb', line 496

def radio_button_tag(name, value, *args)
  if args.length >= 3
    raise ArgumentError, "wrong number of arguments (given #{args.length + 2}, expected 2..4)"
  end
  options = args.extract_options!
  checked = args.empty? ? false : args.first
  html_options = { "type" => "radio", "name" => name, "id" => "#{sanitize_to_id(name)}_#{sanitize_to_id(value)}", "value" => value }.update(options.stringify_keys)
  html_options["checked"] = "checked" if checked
  tag :input, html_options
end