title: How To Submit Multiple Check Boxes With the Same Name When Some Check Boxes Aren't Checked
body: <p>I have interesting situation that I've never had to deal before with Rails (although, it's really an HTML form problem).  I have a table and each row in the table has some form elements.  The user can dynamically add/remove rows as needed so the form is pretty flexible.</p>
  
  <pre><code>&lt;table&gt;
      &lt;tr&gt;
          &lt;%= select_tag .... -%&gt;
          &lt;%= check_box_tag "blah[]", "1", false -%&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;%= select_tag .... -%&gt;
          &lt;%= check_box_tag "blah[]", "1", false -%&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>My problem is with the check boxes. I want to get an array of values for the checkboxes on the server side and I'm running into problems when check boxes are <strong>unchecked</strong> (because HTML doesn't post unchecked check box values).</p>
  
  <p>Say the user checks the first checkbox and doesn't check the second. Ideally, on the server side, I would get</p>
  
  <pre><code>params["blah"] == ["1", "0"]
  </code></pre>
  
  <p>Unforunately, because HTML doesn't send back the unchecked value, what I get is</p>
  
  <pre><code>params["blah"] == ["1"]
  </code></pre>
  
  <p>My first thought was to add </p>
  
  <pre><code>&lt;%= hidden_field_tag "blah[]", "0" -%&gt;
  </code></pre>
  
  <p>before each checkbox, but that of course doesn't work.  I end up with the following on the server side</p>
  
  <pre><code>params["blah"] == ["0", "1", "0"]
  </code></pre>
  
  <p>The hidden fields just always send back "0".  Typically you would use the hidden field trick when you just have one checkbox with a given name.  Now that it's an array of checkboxes values, the hidden field trick appears useless.</p>
  
  <p>Is there an elegant solution to this problem?  One hack would be to change the value of unchecked checkboxes to "0" and force them to be checked via JavaScript when the form is submitted.  That would ensure I get an ordered list back of "1"'s and "0"'s on the server side, but it seems like there has to be a better way.</p>
  
  <p>Other than JS, the only other thing I can think to do is restructure how the data is sent to the server.  Right now I'm sending back several arrays and I'm expecting the arrays to be in order (for example, the first element in each array holds a piece of data for row 1).  Maybe it would work better if I thought in terms of a row and had the client send one big list of rows.  Each entry in the array would contain values for a row.  It would probably mean that each checkbox would have a unique name (based on the row number) so the values could roll up into the right row.   Then I could go back to using the hidden field trick.  Rails can roll all this up into an array of rows (with a hash of data at each row) easily.  But it's probably more work than the JS solution because the form input name attributes will have to be updated each time rows are added and removed (which is why I went with the current approach).</p>
  
  <p>Is there another trick to using check boxes that would make what I'm trying to do easy?</p>