How to use radio button correctly in rails?

GiH picture GiH · Feb 25, 2013 · Viewed 71.1k times · Source

I'm trying to create some radio buttons and I'm not sure how to. Following this question I've got it set up working almost correct, but I'm new to this and not sure why I can't figure it out completely. So what I'm doing is putting a label to group the boolean and then have radio buttons which are labeled Yes and No. If the user clicks the label of Yes it should select the radio button yes (right now they can only click the button itself). This is my code as follows:

  <div class="field">
    <%= f.label :autolyse %><br />
    <%= f.label :autolyse, "Yes", :value => "Yes"  %>
    <%= f.radio_button :autolyse, true%>
    <%= f.label :autolyse, "No", :value => "No" %>
    <%= f.radio_button :autolyse, false, :checked => true %>
  </div>

The first label is for the group, it labels the group "Autolyse". Then I want a Label for "Yes", which if selected will will set true, and then obviously the next one is for False. How do I get this set up correctly?

Answer

shweta picture shweta · Feb 25, 2013

see label(object_name, method, content_or_options = nil, options = nil, &block)

  <div class="field">
    <%= f.label :autolyse %><br />
    <%= f.label :autolyse, "Yes", :value => "true"  %>
    <%= f.radio_button :autolyse, true %>
    <%= f.label :autolyse, "No", :value => "false" %>
    <%= f.radio_button :autolyse, false, :checked => true %>
  </div>