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" })

2 Responses to “About Rails Checkboxes”
  1. shawn says:

    thats hideous! They surly could have done something easier peoples bandwidth. If it can tell you and pass that zero only if the check-box has been interacted with then great but otherwise its just some bloat because people dont want to have to deal with it on the server side

  2. method says:

    I see your point, Shawn, but there are a lot worse ways to waste user’s bandwidth (I’m pretty ashamed of the html + redundant javascript in my other post). I think the Rails guys felt they were “fixing” html form posting. Sending the “0″ is sort of the equivalent of an onUnclick event. I admit that it’s just pushing code around, though.

Leave a Reply