1

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?

7stud
  • 46,922
  • 14
  • 101
  • 127

2 Answers2

3

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|
7stud
  • 46,922
  • 14
  • 101
  • 127
0

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. :)

Whooper
  • 575
  • 4
  • 20
  • No change. I'm wondering if `item_wrapper_tag` is not rails. I can't find any rails docs that explain the recognized options, and I got lost looking through the rails source code. – 7stud Feb 05 '18 at 21:38
  • Do you just want to show the buttons with newlines? – Whooper Feb 05 '18 at 21:40
  • No. I want to wrap each radio button in a div. – 7stud Feb 05 '18 at 21:40
  • @7stud I edited my solution. Please do give it a try. It worked with me and hopefully it also works with you. :) – Whooper Feb 05 '18 at 22:08
  • I already know about simple_form. I just find it hard to believe that a rails form helper forces you to have all your radio buttons on one line. – 7stud Feb 05 '18 at 22:15
  • That's true! Though, there's a solution where you can have your buttons stacked (not on one line) but they won't be wrapped around in a div class. – Whooper Feb 05 '18 at 22:22