title: How can I make a check_box_tag to post a 'false' or '0' parameter when unchecked?
body: <p>With the following check_box_tag:</p>
  
  <pre><code>&lt;%= check_box_tag 'object[boolean_attribute]', 1, object.boolean_attribute %&gt;
  </code></pre>
  
  <p>I can update the boolean_attribute in only one direction: from false to true.</p>
  
  <p>When is unchecked by default (because object.boolean_attribute is false) and I check it and then submit the form, a :boolean_attribute => 1 parameter is posted.</p>
  
  <p><strong>But</strong>, when I try to update from true to false no param is passed, so the boolean_attribute remains true.</p>
  
  <p>In other words, when is checked by default (because object.boolean_attribute is true) and I uncheck it and then submit the form, a :boolean_attribute => 0 is <strong>not</strong> posted.</p>
  
  <p>How can I make this check_box_tag to post a :boolean_attribute => 0 parameter when unchecked?</p>
  
  <p>From the api I can't figure out if there is some option to pass to easily achieve it:
  <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag" rel="noreferrer">http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag</a></p>
  
  <p>Thank you.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>For some reason I cannot fathom, in my actual code (with a nested many-to-many association) the hidden_field_tag is not working.</p>
  
  <pre><code>&lt;%= hidden_field_tag 'order[preparations_attributes][][cooked]', nil %&gt;
  &lt;%= check_box_tag 'order[preparations_attributes][][cooked]', '1', preparation.cooked? %&gt;
  </code></pre>
  
  <p>Now I have the opposite problem: I can uncheck the checkbox and the preparation is updated as aspected, but if I check the checkbox it messes up the params.</p>
  
  <p>Here are the posted params for the unchecked box:</p>
  
  <pre><code>Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"bGgPGbk+Cuk2q+LEgqetmk4e7xie8dB3iMP9Cj3SUm0=", "order"=&gt;{"customer_name"=&gt;"Duccio Armenise", "duedate"=&gt;"2012-04-25 09:24:00.000000", "preparations_attributes"=&gt;[{"quantity"=&gt;"1", "description"=&gt;"custom recipe", "kind"=&gt;"custom", "cooked"=&gt;"", "recipe_id"=&gt;"9", "id"=&gt;"86", "quantities_attributes"=&gt;[{"ingredient_id"=&gt;"", "qty"=&gt;"", "_destroy"=&gt;"0"}, {"ingredient_id"=&gt;"11", "qty"=&gt;"5.0", "id"=&gt;"193", "_destroy"=&gt;"0"}], "_destroy"=&gt;"0"}], "add_preparation"=&gt;{"recipe_id"=&gt;""}}, "continue"=&gt;"Confirm", "id"=&gt;"31"}
  </code></pre>
  
  <p>Now see what a mess when I check the checkbox, beginning from "cooked"=>" ", for some reason Rails is closing the preparation_attributes hash too early!</p>
  
  <pre><code>Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"bGgPGbk+Cuk2q+LEgqetmk4e7xie8dB3iMP9Cj3SUm0=", "order"=&gt;{"customer_name"=&gt;"Duccio Armenise", "duedate"=&gt;"2012-04-25 09:24:00.000000", "preparations_attributes"=&gt;[{"quantity"=&gt;"1", "description"=&gt;"custom recipe", "kind"=&gt;"custom", "cooked"=&gt;""}, {"cooked"=&gt;"1", "recipe_id"=&gt;"9", "id"=&gt;"86", "quantities_attributes"=&gt;[{"ingredient_id"=&gt;"", "qty"=&gt;"", "_destroy"=&gt;"0"}, {"ingredient_id"=&gt;"11", "qty"=&gt;"5.0", "id"=&gt;"193", "_destroy"=&gt;"0"}], "_destroy"=&gt;"0"}], "add_preparation"=&gt;{"recipe_id"=&gt;""}}, "continue"=&gt;"Confirm", "id"=&gt;"31"}
  </code></pre>
  
  <p><strong>EDIT #2</strong>:</p>
  
  <p>I think I ran into a Rails bug related to deep nested resource forms and param passing: <a href="https://github.com/rails/rails/issues/5937" rel="noreferrer">https://github.com/rails/rails/issues/5937</a></p>
  
  <p>For now I made it to work with a select_tag:</p>
  
  <pre><code>&lt;%= select_tag 'order[preparations_attributes][][cooked]', options_for_select({yes: 1, no: 0}, preparation.cooked? ? 1 : 0) %&gt; 
  </code></pre>
  
  <p>I think that switching to a select_tag in order to avoid the "hidden_field gotcha" is an acceptable workaround. </p>
  
  <p>Anyway, thank you for the answers! </p>