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

前端 未结 1 1757
臣服心动
臣服心动 2021-01-02 16:11

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

creates this html:



        
相关标签:
1条回答
  • 2021-01-02 17:08

    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.

    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.

    More information on this gotcha at APIDock

    0 讨论(0)
提交回复
热议问题