Okay, I figured it out:
<%= form_for(:an_article, url: "blah") do |f| %>
<%= f.collection_radio_buttons(
:author_id, Author.all,
:id,
:name_with_initial,
) do |b|
%>
<div>
<%= b.radio_button %>
<%= b.label %>
</div>
<% end %> #collection_radio_buttons do block
<% end %> #form_for do block
radio_button
and label
are builtin methods for the |b|uilder object:
The argument passed to the block is a special kind of builder for this
collection, which has the ability to generate the label and radio
button for the current item in the collection... Using it, you can
change the label and radio button display order or even use the label
as wrapper...
Additional info:
collection_radio_buttons(object, method,
collection,
value_method, text_method,
options={}, html_options={}, &block)
collection: For each element in collection, a radio button and label tag is created.
value_method: Called on each element in collection, and the return value is assigned to
the value attribute of the radio button.
object.method: If the return value of object.method is equal to the value attribute of a radio button,
the radio button gets a checked="checked" attribute.
text_method: Called on each element in collection, and the return value is used as
the text for the label tag.
options: Unknown purpose.
html_options: Used to specify additional html attributes for the radio button, e.g. {class: 'group1'}
When you use form_for()
, the object
argument is the object encapsulated by f, so you omit the object argument:
f.collection_radio_buttons(method,
collection,
value_method, text_method,
options={}, html_options={}, &block)
and method
is called on this object:
|
V
form_for(:an_article, url: "blah") do |f|