Ruby-on-rails 删除“utf8=?” 来自 Rails 3 表单提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4487796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing "utf8=?" from Rails 3 form submissions
提问by grautur
I have a simple search form in my Rails 3 app:
我的 Rails 3 应用中有一个简单的搜索表单:
<%= form_tag search_path, :method => "get" do %>
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "search", :name => nil %>
<% end %>
When the user hits the submit button, they get taken to the URL: http://myapp.com/search?utf8=%E2%9C%93&q=foobar(where %E2%9C%93gets displayed as a checkmark: ?).
当用户点击提交按钮时,他们会被带到 URL:http%E2%9C%93://myapp.com/search?utf8=%E2%9C%93&q= foobar(显示为复选标记的地方:?)。
I'm not doing anything with the utf8parameter, so I want to keep the URL clean by removing it entirely. That is, I want users to get taken to the URL: http://myapp.com/search?q=foobarinstead.
我没有对utf8参数做任何事情,所以我想通过完全删除它来保持 URL 干净。也就是说,我希望用户转至 URL:http: //myapp.com/search?q=foobar。
How do I do this?
我该怎么做呢?
采纳答案by cdmwebs
Even though you aren't doing anything with the parameter, Rails is. It's to correct some issues in IE's parameter encoding. Yehuda has more info here:
即使您没有对参数做任何事情,Rails 也是如此。修正IE的参数编码中的一些问题。Yehuda 在这里有更多信息:
回答by Andrew
Once you understand the purpose of the Rails UTF-8 param, and for some reason you still need to remove it, the solution is easier than you think...just don't use the form_taghelper:
一旦您了解Rails UTF-8 参数的用途,并且出于某种原因仍然需要删除它,解决方案比您想象的要容易……只是不要使用form_tag帮助程序:
<form action="<%= search_path %>" method="get">
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "search", :name => nil %>
</form>
回答by Paul Annesley
form_tagin Rails 4.2(and probably earlier) has a :enforce_utf8option;
form_tag在 Rails 4.2(可能更早)中有一个:enforce_utf8选项;
If set to false, a hidden input with name utf8 is not output.
如果设置为 false,则不会输出名称为 utf8 的隐藏输入。
回答by Jarl
There is gem (utf8_enforcer_workaround) for applying the utf8 param only for non standards complying browsers (or any other logic for that sake). This is handy if you don't want to bother your well behaving users with IE workarounds.
有 gem ( utf8_enforcer_workaround) 用于仅对不符合标准的浏览器(或任何其他逻辑)应用 utf8 参数。如果您不想用 IE 变通方法打扰行为良好的用户,这会很方便。
回答by Henrik N
I made an initializer to remove the param from GET requests. It's obviously a hack.
我做了一个初始化程序来从 GET 请求中删除参数。这显然是一个黑客。
This goes in config/initializers/overrides.rb:
这进入config/initializers/overrides.rb:
# Don't let Rails add a utf8=? param to GET forms.
# See http://stackoverflow.com/questions/3222013/what-is-the-snowman-param-in-rails-3-forms-for for details.
module ActionView::Helpers::FormTagHelper
private
def extra_tags_for_form_with_utf8_param_excluded_from_gets(html_options)
old = extra_tags_for_form_without_utf8_param_excluded_from_gets(html_options)
non_get = old.include?('"_method"') || old.include?('"'+request_forgery_protection_token.to_s+'"')
if non_get
old
else
old.sub(/<[^>]+name="utf8"[^>]+"✓"[^>]*>/, '').html_safe
end
end
alias_method_chain :extra_tags_for_form, :utf8_param_excluded_from_gets
end
Ideally, Rails should probably have a setting for this, or at least rewrite extra_tags_for_form so it's less messy to patch.
理想情况下,Rails 应该为此设置一个设置,或者至少重写 extra_tags_for_form 以便修补时不那么麻烦。
回答by Jan
Try this in your ApplicationController:
在你的 ApplicationController 中试试这个:
def default_url_options(options={})
options.delete('utf8')
end
回答by Ken Hibino
Using before_action and redirecting to another action worked for me. For example, if you are searching for posts, set up a route for search on collection.
使用 before_action 并重定向到另一个动作对我有用。例如,如果您正在搜索帖子,请设置搜索集合的路径。
resources :posts do
collection do
get 'search'
end
end
and make HTTP GET request for posts#index action.
并为 posts#index 操作发出 HTTP GET 请求。
<%= form_tag posts_path, method: :get do %>
<%= search_field_tag :q, params[:q], placeholder: "Search posts" %>
<%= submit_tag "Go" %>
<% end %>
and then in PostsController,
然后在 PostsController 中,
before_action :check_for_query, only: :index
...
private
def check_for_query
redirect_to articles_search_url(q: params[:q]) if params[:q].present?
end
and call Post.search in posts#search action and render index page.
并在 posts#search 操作中调用 Post.search 并呈现索引页面。
def search
Post.search(params[:q])
render :index
end
The URL will look like /posts/search?q=foo
URL 看起来像 /posts/search?q=foo
回答by Akiko
I think we should use: enforce_utf8: falsein form_tag.
我认为我们应该使用:enforce_utf8: false在 form_tag 中。
Ex:
前任:
= form_tag search_path, method: :get, id: 'searchForm', enforce_utf8: false

