Why does the check_box form helper generate two flags, one is hidden?

this code: = form_fo: store_products do | f | = f.check_box: track_inventory

creates this html:

<input name="product_group[products_attributes][0][store_products_attributes}[1342647745501][track_inventory]" type="hidden" value="0"> <input id="product_group_products_attributes_0_store_products_attributes_1342647745501_track_inventory" name="product_group[products_attributes][0][store_products_attributes][1342647745501][track_inventory]" type="checkbox" value="1"> 

What is the reason for the first hidden element?

+7
source share
1 answer

The HTML specification states that checkboxes without a checkbox should not be sent by web browsers. This means that if it is not installed, the rails do not receive records about whether the check box is unchecked. This would be important, for example, if the user edited the record in which the checkbox was set, and they decided to uncheck the box - the rails would not know to update this attribute.

The hidden field has the same name as the flag, so if the flag is not sent, hidden_field is sent instead (with a value of "0", which means uncontrolled). Thus, the rails will always receive a signal about whether the flag has been checked or unchecked.

For more information on this, go to APIDock.

+21
source

All Articles