How can I get a set of radio buttons to accept NULL (nothing checked)?
        Posted  
        
            by Ethan
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Ethan
        
        
        
        Published on 2010-03-23T20:42:43Z
        Indexed on 
            2010/03/23
            20:43 UTC
        
        
        Read the original article
        Hit count: 183
        
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.)
© Stack Overflow or respective owner