Method: ActionView::Helpers::FormTagHelper#check_box_tag

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

#check_box_tagObject

:call-seq:

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

Creates a check box form input tag.

Options

  • :value - The value of the input. Defaults to "1".

  • :checked - If set to true, the checkbox will be checked 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

checkbox_tag 'accept'
# => <input id="accept" name="accept" type="checkbox" value="1" />

checkbox_tag 'rock', 'rock music'
# => <input id="rock" name="rock" type="checkbox" value="rock music" />

checkbox_tag 'receive_email', 'yes', true
# => <input checked="checked" id="receive_email" name="receive_email" type="checkbox" value="yes" />

checkbox_tag 'tos', 'yes', false, class: 'accept_tos'
# => <input class="accept_tos" id="tos" name="tos" type="checkbox" value="yes" />

checkbox_tag 'eula', 'accepted', false, disabled: true
# => <input disabled="disabled" id="eula" name="eula" type="checkbox" value="accepted" />


466
467
468
469
470
471
472
473
474
475
# File 'actionview/lib/action_view/helpers/form_tag_helper.rb', line 466

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