Set a value of 0 for unchecked checkboxes

Many people who aren’t too familiar with the Ruby on Rails framework may do well to learn about a convenient little trick they do with submitting check boxes.

In HTML, when you click a check box and submit it, by default, the value POSTed is “1” – and if you do not click it, nothing is submitted. This sometimes makes keeping track of check boxes difficult.

To combat this, Rails puts a hidden field before the check box, with the same name as the check box, with a value of zero.


Click this to set my_ckbx to 1 

Now when the form loads, my_chbx is defaulted to “0”. If you choose to click the check box, my_ckbx will be set to “1”, if not, it will remain “0”. No matter what, my_ckbx will POST a value.

Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *