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!

About these ads

10 Responses to “Radio Button Info for Ruby on Rails”

  1. anonymous coward Says:

    :) thanks


  2. […] 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 […]

  3. Marcos Says:

    Thank you very much ! That’s exactly what I was looking for.

  4. khat Says:

    Thank you a lot for the article!

  5. rblunt Says:

    Does this still work, or has it been changed?

  6. Ajeje Says:

    Thanks, it work!

  7. nelson Says:

    Why are the simplest things missing from documentation all the time? oodles of thanks for taking the time to post!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: