I’m trying to test whether a check_box in my rails app is checked.
Here’s the code in my model:
attr_accessor :post_to_facebook
before_save :to_facebook?
def to_facebook?
if self.post_to_facebook
// do stuff
end
end
And in the view:
<div class="field">
<%= f.check_box :post_to_facebook, :checked => 'checked' %><%= f.label :post_to_facebook %>
</div>
Checked or not, Rails is always evaluating to_facebook as true. For example, this is what was posted in the log: "post_to_facebook"=>"0", but it still evaluated as true. Can anyone spot what’s wrong with the code?
In Ruby, only
falseandnilare evaluated asfalse. That means"0"(or even0) is “truthy”. What you can do is :