为什么check_box表单助手会生成两个复选框,一个隐藏?

时间:2021-10-03 09:51:12

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

此代码:= form_fo:store_products do | f | = f.check_box:track_inventory

creates this html:

创建这个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?

第一个隐藏元素的原因是什么?

1 个解决方案

#1


21  

The HTML specification says that unchecked checkboxes should not be sent by webbrowsers. This means that, if unchecked, rails receives no record of whether the checkbox on the form was unchecked. This would be important, for example, if the user was editing a record where the checkbox was previously checked and they had decided to uncheck it - rails would not know to update this attribute.

HTML规范说不应该由webbrowsers发送未选中的复选框。这意味着,如果未选中,则rails不会收到表单上的复选框是否未选中的记录。这很重要,例如,如果用户正在编辑先前已选中复选框并且他们已决定取消选中该复选框的记录,则rails将无法更新此属性。

The hidden field has the same name as the checkbox, so if the checkbox is not sent, the hidden_field is sent instead (with the value of '0', meaning unchecked). This way, rails will always receive a signal as to whether the checkbox was checked or unchecked.

隐藏字段与复选​​框具有相同的名称,因此如果未发送复选框,则会发送hidden_​​field(值为“0”,表示未选中)。这样,rails将始终收到有关复选框是选中还是未选中的信号。

More information on this gotcha at APIDock

有关此问题的更多信息,请访问APIDock

#1


21  

The HTML specification says that unchecked checkboxes should not be sent by webbrowsers. This means that, if unchecked, rails receives no record of whether the checkbox on the form was unchecked. This would be important, for example, if the user was editing a record where the checkbox was previously checked and they had decided to uncheck it - rails would not know to update this attribute.

HTML规范说不应该由webbrowsers发送未选中的复选框。这意味着,如果未选中,则rails不会收到表单上的复选框是否未选中的记录。这很重要,例如,如果用户正在编辑先前已选中复选框并且他们已决定取消选中该复选框的记录,则rails将无法更新此属性。

The hidden field has the same name as the checkbox, so if the checkbox is not sent, the hidden_field is sent instead (with the value of '0', meaning unchecked). This way, rails will always receive a signal as to whether the checkbox was checked or unchecked.

隐藏字段与复选​​框具有相同的名称,因此如果未发送复选框,则会发送hidden_​​field(值为“0”,表示未选中)。这样,rails将始终收到有关复选框是选中还是未选中的信号。

More information on this gotcha at APIDock

有关此问题的更多信息,请访问APIDock