Radio buttons on Rails

Ruby on-RailsRubyWebformsRadio Button

Ruby on-Rails Problem Overview


Similar to this question: https://stackoverflow.com/questions/621340/checkboxes-on-rails

What's the correct way of making radio buttons that are related to a certain question in Ruby on Rails? At the moment I have:

<div class="form_row">
    <label for="theme">Theme:</label>
    <br><%= radio_button_tag 'theme', 'plain', true %> Plain
    <br><%= radio_button_tag 'theme', 'desert' %> Desert
    <br><%= radio_button_tag 'theme', 'green' %> Green
    <br><%= radio_button_tag 'theme', 'corporate' %> Corporate
    <br><%= radio_button_tag 'theme', 'funky' %> Funky
</div>

I also want to be able to automatically check the previously selected items (if this form was re-loaded). How would I load the params into the default value of these?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

As in this previous post, with a slight twist:

<div class="form_row">
    <label for="theme">Theme:</label>
    <% [ 'plain', 'desert', 'green', 'corporate', 'funky' ].each do |theme| %>
      <br><%= radio_button_tag 'theme', theme, @theme == theme %>
      <%= theme.humanize %>
    <% end %>
</div>

Where

@theme = params[:theme]

Solution 2 - Ruby on-Rails

Same as V's, but has associated labels with each radio button. Clicking the label checks the radio button.

<div class="form_row">
  <p>Theme:</p>
  <% [ 'plain', 'desert', 'green', 'corporate', 'funky' ].each do |theme| %>
    <br><%= radio_button_tag 'theme', theme, @theme == theme %>
    <%= label_tag "theme_#{theme}", theme.humanize %>
  <% end %>
</div>

Solution 3 - Ruby on-Rails

Using Haml, getting rid of needless br tags, and nesting inputs within label so that they may be selected without matching labels to ids. Also using form_for. I would consider this to be following best practices.

= form_for current_user do |form|
  .form_row
    %label Theme:
    - [ 'plain', 'desert', 'green', 'corporate', 'funky' ].each do |theme|
      %label
        = form.radio_button(:theme, theme)
        = theme.humanize

Solution 4 - Ruby on-Rails

I would suggest having a look at formtastic

It makes radio button and check box collections vastly easier and more concise. Your code would look like so:

    <% semantic_form_for @widget, :html => {:class => 'my_style'} do |f| %>
<%= f.input :theme, :as => :radio, :label => "Theme:", 
:collection =>  [ 'plain', 'desert', 'green', 'corporate', 'funky' ] %>
<% end %>


Formtastic is largely unobtrusive and can be mixed and matched with the "classic" form builders. You can also override the formtastic css class for the form as I did above with
:html => {:class => 'my_style'}

Have a look at the pertinent Railscasts.

Update: I've recently moved to Simple Form which has similar syntax to formtastic but is more lightweight and especially leaves the styling to your own css.

Solution 5 - Ruby on-Rails

Hmm, from the docs I don't see how you can set the ID on the radio buttons... the label's for attribute tries to link to the ID on the radio.

rails docs for radio_button_tag

That said, from the doc, that first param is the "name"... which if that is what it is creating, should group them alltogether. If not, maybe its a bug?

Hmm, wonder if these have been fixed: http://dev.rubyonrails.org/ticket/2879 http://dev.rubyonrails.org/ticket/3353

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalamodeyView Question on Stackoverflow
Solution 1 - Ruby on-RailsvladrView Answer on Stackoverflow
Solution 2 - Ruby on-RailsdazonicView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDaniel X MooreView Answer on Stackoverflow
Solution 4 - Ruby on-RailsdirkbView Answer on Stackoverflow
Solution 5 - Ruby on-RailsscunliffeView Answer on Stackoverflow