ActionView::Helpers::FormOptionsHelper

collection_checkboxes

API docs (Rails 8.1.4.1) ↗

Alias: collection_check_boxes

def collection_checkboxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)

Description

Creates a checkbox and a <label> for each item in collection, bound to a model attribute.

Arguments

Changing a value updates the ERB and HTML.

object:user
method:plan_ids
collectionPlan.all
Calls this method on each item in collection and sets the value of the checkbox to the return value. Also appends it to the id. With a proc, calls it with the item and uses the return value. Adds checked to the checkboxes whose value is in the object's value (an array). The comparison with the object's value includes the type, so if this method returns numbers and the object's value is an array of strings such as ["1", "3"], no checked is added (the :checked option compares as strings).
Calls this method on each item in collection and uses the return value as the content of the <label>. With a proc, calls it with the item and uses the return value.
options= {}
[2]
Instead of using the object's value, adds checked to each checkbox whose value matches the given value (or, for an array, any value in it). No other checkbox gets it. With a proc, calls it for each item in collection and adds checked to the checkbox of each item for which it returns a truthy value.
->(plan) { plan.archived? }
Adds disabled to each checkbox whose value matches the given value (or, for an array, any value in it). With a proc, calls it for each item in collection and adds disabled to the checkbox of each item for which it returns a truthy value.
false omits the hidden <input> at the top.
Options not listed here do not become HTML attributes, except :readonly and :selected: like :disabled, they add that attribute to the checkboxes whose value matches the given value.
html_options= {}
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

"q"
Replaces the generated name with the given value. nil omits name.Also used as the name attribute of the hidden <input> at the top. No [] is appended, so to send multiple values, write it as q[].

Shared by all helpers

true adds disabled="disabled"; false omits it. The same goes for every HTML attribute in Rails' list (BOOLEAN_ATTRIBUTES), such as readonly, required and autofocus: any value other than false or nil adds it as attr="attr".
{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 html_options become HTML attributes of every checkbox; those with a nil value are left out.
&block= nil
With a block, calls it for each item and outputs its result in place of the checkbox and <label>. On the block argument (b), checkbox and label create the checkbox and <label> for that item, text and value return what text_method and value_method returned, and object returns the item itself.

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