Radio Button Info for Ruby on Rails
October 2, 2007
I wanted to set a default, checked radio button in a group of radio buttons. Though Agile Development w/Rails is a great book, it doesn’t tell you much about the radio buttons. And most of the examples on the web where unsatisfactory for finding this little detail. Perhaps my search queries were poorly written, but given that I had trouble finding the solution it will probably be valuable to post my own.
The problem lies in that I didn’t quite understand this: “If the current value of method is tag_value the radio button will be checked.” – Rails API for Radio Button Besides, I wanted to arbitrarily set which button was checked. I knew how to do this in normal html/js, but I waned to pass it the adjustment in RoR. In the end, it was actually pretty simple.
All you do is add “:checked = true“
Your Ruby looks like this:
<%= form.radio_button :ending, “true”, :checked => true %>
And the page source looks like this:
<input checked=“checked” id=“competition_ending_true” name=“competition[ending]” type=“radio” value=“true” />
This should give you a radio button that is checked by default.
If there are better ways to do it or anything else I’ve missed, there’s always room in the comments for improvements. Rock the RoR!