views:

36

answers:

1

I am using Formtastic and I have a simple boolean field. In my view, I have

        <%= f.input :active, :label => "Enabled?", :as => :select, :include_blank => false, :collection => [ ["Yes", true], ["No", false] ] %><br />

It saves to the database just fine. But when it loads, it always shows the first value ("Yes").

What am I missing? It should default to "No" when the field is false.

Thanks for any tips.

EDIT

When I put the ["No", false] first, it works!

        <%= f.input :active, :label => "Enabled?", :as => :select, :include_blank => false, :collection => [ ["No", false], ["Yes", true] ] %>

Why would that matter?????

A: 

What happens if you remove the :include_blank => false? Is the first option (blank) selected? If so, could the value for the attribute be nil and not false?

zetetic
When I remove that, I get three options, "", "true", "false".
cbmeeks
...and which one is selected?
zetetic
The first one. In fact, it's always the first one UNLESS I put the negative (false) as the first in the list. Then it works perfectly. See my EDIT above. I wonder if this is a bug and maybe I should submit it?
cbmeeks
No, I think what's happening is that the value of the attribute is `nil`, not `false`. Unless you have a `default` constraint in the database, or you initialize the value in your model, the default value for a boolean attribute is `nil`. Because you turn off the blank selection with `include_blank` => false, there is no way for the form to indicate that the value is `nil`, so the form just selects the first element in the collection. Normally a boolean attribute is displayed using radio buttons or a checkbox, which don't suffer from this problem.
zetetic