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

    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

  • shawn

    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

  • method

    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.

  • method

    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.

  • @shawn

    That’s a pretty ridiculous point to make, unless:

    a) You’re on such a low bandwidth connection that a few hundred bytes of transfer causes noticeable delays (in which case certainly don’t use Ext JS!)
    b) You’re paying for your bandwidth by the byte (again, don’t use Ext JS)

    Are you seriously suggesting that <> will have any noticeable impact on page load speed? That string of text can be represented using 72 bytes, which will easily fit in a packet along with a lot more page data. As a percentage of the size of the Ext JS library – which is of course required to even run the example – this comes to about 0.013%, depending on whether you load the full library or just cherry pick features to save bandwidth.

    In any case this was a post about Rails checkboxes, and this is precisely how Rails expects checkbox data to arrive.

    Nice post. Buried deep in my todo list is to write a ux to allow this to be added in one component. e.g.:

    {
    xtype: ‘rails_checkbox’,
    fieldLabel: ‘Field Name’,
    name: ‘model_name[field_name]’
    }

    Which would be equivalent to:

    {
    xtype: ‘checkbox’,
    fieldLabel: ‘Field Name’,
    name: ‘model_name[field_name]’,
    inputValue: ‘1’
    },
    {
    xtype: ‘hidden’,
    name: ‘model_name[field_name]’,
    value: ‘0’
    }

  • @shawn

    That’s a pretty ridiculous point to make, unless:

    a) You’re on such a low bandwidth connection that a few hundred bytes of transfer causes noticeable delays (in which case certainly don’t use Ext JS!)
    b) You’re paying for your bandwidth by the byte (again, don’t use Ext JS)

    Are you seriously suggesting that <> will have any noticeable impact on page load speed? That string of text can be represented using 72 bytes, which will easily fit in a packet along with a lot more page data. As a percentage of the size of the Ext JS library – which is of course required to even run the example – this comes to about 0.013%, depending on whether you load the full library or just cherry pick features to save bandwidth.

    In any case this was a post about Rails checkboxes, and this is precisely how Rails expects checkbox data to arrive.

    Nice post. Buried deep in my todo list is to write a ux to allow this to be added in one component. e.g.:

    {
    xtype: ‘rails_checkbox’,
    fieldLabel: ‘Field Name’,
    name: ‘model_name[field_name]’
    }

    Which would be equivalent to:

    {
    xtype: ‘checkbox’,
    fieldLabel: ‘Field Name’,
    name: ‘model_name[field_name]’,
    inputValue: ‘1’
    },
    {
    xtype: ‘hidden’,
    name: ‘model_name[field_name]’,
    value: ‘0’
    }