Rails 3 表单助手:UTF8 和其他隐藏字段

发布于 2024-09-26 07:15:22 字数 1980 浏览 2 评论 0原文

观点:

<%= form_for :blog_post do |f| %>
  <ul>
    <li>
      <%=  f.label :title %>
      <%= f.text_field :title, :type => 'text', :id => 'title', :size => '', :limit => '255' %>
    </li>

  </ul>
<% end %>

<!DOCTYPE html> 
    <html> 
    <head> 
      <title>LevihackwithCom</title> 
      <script src="/javascripts/prototype.js?1285902540" type="text/javascript"></script> 
      <script src="/javascripts/effects.js?1285902540" type="text/javascript"></script> 
      <script src="/javascripts/dragdrop.js?1285902540" type="text/javascript"></script> 
      <script src="/javascripts/controls.js?1285902540" type="text/javascript"></script> 
      <script src="/javascripts/rails.js?1285902540" type="text/javascript"></script> 
      <script src="/javascripts/application.js?1285902540" type="text/javascript"></script> 
      <meta name="csrf-param" content="authenticity_token"/> 
      <meta name="csrf-token" content="UnhGSHHanJHfgJYhnksqJ1bfq3W+QEU2GJqLAMs2DmI="/> 
    </head>

    <body> 

    <form accept-charset="UTF-8" action="/blog_post/new" method="post">
      <div style="margin:0;padding:0;display:inline">
        <input name="utf8" type="hidden" value="&#x2713;" />
        <input name="authenticity_token" type="hidden" value="UnhGSHHanJHfgJYhnksqJ1bfq3W+QEU2GJqLAMs2DmI=" />
      </div> 
      <ul> 
        <li> 
          <label for="blog_post_title">Title</label> 
          <input id="title" limit="255" name="blog_post[title]" size="" type="text" /> 
        </li> 
      </ul> 
    </form> 

    </body> 
    </html> 

我正在摆弄表单助手。上面的代码显示了我的视图文件以及它生成的 HTML。可怕的 div 充满了内联 CSS,里面塞满了我没有明确要求的隐藏字段,这是怎么回事?哪些设置会导致生成这些字段?有没有办法删除内联 CSS?

The view:

<%= form_for :blog_post do |f| %>
  <ul>
    <li>
      <%=  f.label :title %>
      <%= f.text_field :title, :type => 'text', :id => 'title', :size => '', :limit => '255' %>
    </li>

  </ul>
<% end %>

<!DOCTYPE html> 
    <html> 
    <head> 
      <title>LevihackwithCom</title> 
      <script src="/javascripts/prototype.js?1285902540" type="text/javascript"></script> 
      <script src="/javascripts/effects.js?1285902540" type="text/javascript"></script> 
      <script src="/javascripts/dragdrop.js?1285902540" type="text/javascript"></script> 
      <script src="/javascripts/controls.js?1285902540" type="text/javascript"></script> 
      <script src="/javascripts/rails.js?1285902540" type="text/javascript"></script> 
      <script src="/javascripts/application.js?1285902540" type="text/javascript"></script> 
      <meta name="csrf-param" content="authenticity_token"/> 
      <meta name="csrf-token" content="UnhGSHHanJHfgJYhnksqJ1bfq3W+QEU2GJqLAMs2DmI="/> 
    </head>

    <body> 

    <form accept-charset="UTF-8" action="/blog_post/new" method="post">
      <div style="margin:0;padding:0;display:inline">
        <input name="utf8" type="hidden" value="✓" />
        <input name="authenticity_token" type="hidden" value="UnhGSHHanJHfgJYhnksqJ1bfq3W+QEU2GJqLAMs2DmI=" />
      </div> 
      <ul> 
        <li> 
          <label for="blog_post_title">Title</label> 
          <input id="title" limit="255" name="blog_post[title]" size="" type="text" /> 
        </li> 
      </ul> 
    </form> 

    </body> 
    </html> 

I'm messing around with form helpers. The above code shows my view file as well as the HTML it generates. What is with the terrible div full of inline CSS stuffed with hidden fields I didn't explicitly ask for? What settings cause these fields to be generated? Is there a way for me to remove the inline CSS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怼怹恏 2024-10-03 07:15:22

这些字段以 Rails 表单的形式生成,以确保稳健性:

utf8=✓

utf8 隐藏字段可确保表单值以 UTF8 格式提交。它通过确保提交表单中至少一个 UTF8 字符来实现这一点。大多数浏览器都会尊重文档的编码并以相同的方式对待表单值,但有一种浏览器存在问题。因此,utf8 得到了复选标记。

authenticity_token 的作用是防止跨站请求伪造。

为复选框生成类似的隐藏字段。由于未选中的复选框不会提交到服务器,因此隐藏字段可确保提交“0”(假)值:当您有一组复选框时,这非常有用。

这些字段被包裹在具有内联样式的 div 中,以确保它们不会破坏布局。您可以浏览表单助手源代码并覆盖它,但我不推荐它:它的侵入性很小,而且它的存在是有原因的。

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.

缱倦旧时光 2024-10-03 07:15:22

如果您想摆脱 utf8=✓ 您可能会对这个 gem 感兴趣,它仅将其添加到不兼容的浏览器中:
https://github.com/softace/utf8_enforcer_workaround

If you want to get rid of the utf8=✓ you may be interested in this gem, which adds it only to non-complying browsers:
https://github.com/softace/utf8_enforcer_workaround

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文