Rails 3 Form Helpers: UTF8 and other hidden fields

前端 未结 2 1178
囚心锁ツ
囚心锁ツ 2021-02-04 14:42

The view:

<%= form_for :blog_post do |f| %>
  
  • <%= f.label :title %> <%= f.text_field :title, :type => \'
2条回答
  •  不知归路
    2021-02-04 15:17

    These fields are generated in rails forms for robustness:

    utf8=✓

    The utf8 hidden field ensures that the form values are submitted as UTF8. It does this by ensuring that at least one UTF8 character in the form gets submitted. Most browsers respect the document's encoding and treat the form values the same, but there's one browser that has a problem. Hence, utf8 gets a checkmark.

    The authenticity_token is there to prevent cross-site request forgery.

    Similar hidden fields get generated for checkboxes. Since unchecked checkboxes don't get submitted to the server, a hidden field ensures that a "0" (false) value gets submitted: this is helpful when you have an array of checkboxes.

    These fields get wrapped in a div with inline styles to ensure that they don't break the layout. You could poke around in the form helper source code and override this, but I wouldn't recommend it: it's minimally intrusive, and it's there for a reason.

提交回复
热议问题