Rails 3 Form Helpers:UTF8和其他隐藏字段 [英] Rails 3 Form Helpers: UTF8 and other hidden fields

查看:67
本文介绍了Rails 3 Form Helpers:UTF8和其他隐藏字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视图:

<%= 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.充满内联CSS的可怕div充满了我没有明确要求的隐藏字段,这是怎么回事?哪些设置会导致生成这些字段?我有办法删除内联CSS吗?

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?

推荐答案

这些字段以rails形式生成,以提高稳定性:

These fields are generated in rails forms for robustness:

utf8=✓

utf8隐藏字段可确保将表单值提交为UTF8.它通过确保提交表单中的至少一个UTF8字符来做到这一点.大多数浏览器都遵循文档的编码,并且将表单值视为相同,但是有一种浏览器存在问题.因此,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.

使用authenticity_token可以防止跨站点请求伪造.

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

为复选框生成相似的隐藏字段.由于未选中的复选框不会提交到服务器,因此隐藏字段可确保提交"0"(假)值:当您具有多个复选框时,这很有用.

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.

这些字段以内联样式包装在div中,以确保它们不会破坏布局.您可以在表单帮助程序源代码中查找并覆盖它,但是我不建议您这样做:它具有最小程度的侵入性,并且存在于其中是有原因的.

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.

这篇关于Rails 3 Form Helpers:UTF8和其他隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆