ActionView::Helpers::FormHelper

Alias: check_box

def checkbox(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")

Description

Creates an <input> with type="checkbox" for a model attribute, and a hidden <input> before it.

Arguments

Changing a value updates the ERB and HTML.

object_name:article
method:published
options= {}
Adds checked="checked" only for true or "checked". Unlike disabled and the like, "true" and 1 do not add it. By default, Rails decides by comparing the object's value with the checked_value argument.
"yes"
Ignored. The value attribute is set from the checked_value argument.
false omits the hidden <input>.
true appends [] to the name attribute of both the checkbox <input> and the hidden <input>. It also appends the checked_value argument to the id, with spaces and . replaced by _, punctuation other than - removed, and the result downcased. Does not add the multiple attribute. Meant for internal use by collection_checkboxes.
true adds disabled to both the checkbox <input> and the hidden <input>.
"other_form"
Also adds the same form to the hidden <input>.
"q"
Replaces the generated name attribute. Also adds the same name to the hidden <input>. nil omits name from both.
Common options

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

Shared by form helpers

nil
Replaces the generated id with the given value. nil omits id.

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.
{describedby: ["name_help", "name_error"], invalid: false}
Like :data, adds aria-* attributes. Array values are joined with spaces, leaving out nil and empty strings (nothing is added if none remain). For a hash value, the keys whose values are not false or nil are joined with spaces.
Any other options become HTML attributes of the <input type="checkbox">; those with a nil value are left out.
= "1"
Used as the value attribute of the checkbox <input>.
= "0"
Used as the value attribute of the hidden <input>. nil or false omits the hidden <input>.

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