Tuesday, March 23, 2010

How can I get a set of radio buttons to accept NULL (nothing checked)?

Programmer Question

I'm working on a Rails application where I have some a set of two radio buttons where users can click "yes" or "no". The MySQL DB column created by the ActiveRecord migration is a tinyint.



If the user doesn't click either radio button I want MySQL to store NULL. (The column allows NULL.) And when they come back to edit the data, neither button should be checked.



What's happening is that ActiveRecord is storing 0 and then when I come back the "No" button is checked.



Rails 2.3.5



Form code (I'm using Haml):



        = f.radio_button( :model_attribute, true )
Yes

= f.radio_button( :model_attribute, false )
No


(In retrospect it probably would have been better to use a single checkbox, but it would be difficult to change that now.)



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails