views:

934

answers:

3

Hello, I have the code:

<% form_for(@libation) do |f| %>
<%= f.radio_button :carbonated, true %> <%= f.label :carbonated, "Yes" %>
<%= f.radio_button :carbonated, false %> <%= f.label :carbonated, "No" %>
<% end %>

The carbonated column in db is tinyint. And when the above is run and I click "No" I get a big red box telling me:

1 error prohibited this libation from being saved There were problems with the following fields: * Carbonated can't be blank

Any ideas? -thanx

A: 

Hah, answering my own question. Seems there is a problem with HTML spec not sending a "false" properly, so need some work around. For me, in the model file I had:

validates_presence_of :carbonated

and simply removing it fixes this error message. I default to false and it can be changed to true. Error message gone.

ciao.

+1  A: 

I am having the same problem. But, taking out the validation doesn't solve my problem, because I still require that the user choose one or the other. I can't just assume that if they don't choose one of them, it's false. Any other ideas?

davekaro
A: 

I had difficulties with the validation of a boolean value, so i learned to validate their presence you need to do:

validates_inclusion_of :carbonated, :in => [true, false]
ernd enson