Rails checkbox validation error -


i'm running validation error on checkbox field (perishable can't blank) when check box unchecked. can check logs , see "perishable"=>"0" gets passed when unchecked, , "perishable"=>"1" when checked. "perishable" white-listed in controller, , works when checkbox checked. what's going on here?

model:

class product < activerecord::base   validates_presence_of :perishable end 

migration:

class createproducts < activerecord::migration   def change     create_table :products |t|       t.boolean :perishable, :null => false     end   end end 

view:

= f.label :perishable = f.check_box :perishable 

rendered view:

<label for="product_perishable">perishable *</label> <input name="product[perishable]" type="hidden" value="0" /> <input id="product_perishable" name="product[perishable]" type="checkbox" value="1" /> 

you validating presence of boolean field apparently creates problems when false.

as per this answer:

if want validate presence of boolean field (where real values true , false), want use validates_inclusion_of :field_name, :in => [true, false].


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -