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!
Primevector RSS
December 28, 2007 at 9:40 pm
:) thanks
April 8, 2008 at 12:50 am
[...] noticed that resources go much farther than opinion or sentiment on the internet. My only programming post on this blog is about Ruby on Rails. It generates the majority of the visits to this blog because it gained [...]
May 2, 2008 at 2:10 pm
Thank you very much ! That’s exactly what I was looking for.
September 4, 2008 at 1:10 pm
Thank you a lot for the article!
October 21, 2008 at 6:13 am
Thank you !
December 7, 2008 at 11:03 pm
TY!
January 9, 2009 at 10:51 pm
Does this still work, or has it been changed?
January 10, 2009 at 12:44 pm
I have no idea. RoR updates may have fixed this. I’ll have to check it out.
October 26, 2009 at 2:39 pm
Thanks, it work!
May 5, 2010 at 5:59 pm
Why are the simplest things missing from documentation all the time? oodles of thanks for taking the time to post!