collection_radio_buttons: how do you wrap a tag around each radio button?

4k Views Asked by At

collection_radio_buttons() is defined in the rails 5.1 docs like this:

collection_radio_buttons(
    method, collection, 
    value_method, 
    text_method, 
    options = {}, 
    html_options = {}, &block
)

There is no explanation in the docs for what the options argument is. The simple_form docs say that there is an option called item_wrapper_tag.

I've been trying this:

<%= form_for(:an_article, url: "blah") do |f| %>

<%= f.collection_radio_buttons(
  :author_id, Author.all, 
  :id, 
  :name_with_initial,
  {item_wrapper_tag: :div}  #<=== HERE *****
) 
%>

<% end %>

I've tried every combination of symbols and strings for the key, item_wrapper_tag, and the value, div, and nothing succeeds in wrapping each radio button in a div.

Does anyone know if rails has a similar option as item_wrapper_tag?

2

There are 2 best solutions below

0
7stud On BEST ANSWER

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|
6
Whooper On

Try using the gem simple_form for your forms. Then the code below should work already.

  • Add gem simple_form in your Gemfile.
  • Run bundle install
  • Run rails generate simple_form:install

Then create a simple_form in your view that would look like this:

<%= simple_form_for @post do |f| %>

    <%= f.collection_radio_buttons( :author_id, Author.all, :id, :name_with_initial, item_wrapper_tag: :div) %>

<% end %>

Note: I just followed the form from the collection_radio_buttons from APIDock.

This might do the trick. :)