About Rails Checkboxes

This is another boring tip in case you have to construct a form to send to Rails, and I’m sure it’s documented everywhere:

Rails-generated checkboxes return a value of 1 when they’re clicked. That is, the parameters will contain the name of the checkbox and a value 1. But the way checkboxes work is they’re either on or … they’re not included in the parameters. They don’t get sent as “off” or “0”. So the thing to realize is that the Rails-generated checkbox is accompanied by a generated hidden field with the same name as the checkbox and a value of 0. Apparently, if there are two identical names submitted in a form the first one takes priority. Who knew?

So you would do something like this with Ext JS:

new Ext.form.Checkbox({ name : "profile[licensed_realtor]", inputValue : "1",
            id : 'js_profile_licensed_realtor_field', fieldLabel : "Licensed Realtor",
            checked : <%= @user.profile.licensed_realtor == true ? "true" : "false" %> }),
        new Ext.form.Hidden({ name : "profile[licensed_realtor]", value : "0" })
%d bloggers like this: