ActionView::Helpers::FormOptionsHelper

collection_radio_buttons

API docs (Rails 8.1.4.1) ↗
def collection_radio_buttons(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)

Description

Creates a radio button 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_id
collectionPlan.all
Calls this method on each item in collection and sets the value of the radio button 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 radio button whose value matches the object's value.
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 the radio button whose value matches the given value. No other radio button gets it. With a proc, calls it for each item in collection and adds checked to the radio button of the item for which it returns a truthy value.
->(plan) { plan.archived? }
Adds disabled to each radio button 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 radio button 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 radio buttons 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="radio">, 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.

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 radio button; 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 radio button and <label>. On the block argument (b), radio_button and label create the radio button 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