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%93
gets displayed as a checkmark: ✓).
当用户点击提交按钮时,他们会被带到URL: http://myapp.com/search?use utf8 = % E2 % 9 c % 93 q = foobar(% E2 % 9 c % 93显示选中标记:✓)。
I'm not doing anything with the utf8
parameter, 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=foobar instead.
我没有对utf8参数做任何操作,所以我想通过完全删除URL来保持URL的整洁。也就是说,我希望用户可以访问URL: http://myapp.com/search?q = foobar相反。
How do I do this?
我该怎么做呢?
7 个解决方案
#1
24
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在这里有更多信息:
What is the _snowman param in Ruby on Rails 3 forms for?
Ruby on Rails 3中的_snowman param用于什么?
#2
39
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_tag
helper:
一旦您理解了Rails UTF-8 param的用途,并且出于某种原因您仍然需要删除它,那么解决方案比您认为的要简单……不要使用form_tag助手:
<form action="<%= search_path %>" method="get">
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "search", :name => nil %>
</form>
#3
30
form_tag
in Rails 4.2 (and probably earlier) has a :enforce_utf8
option;
Rails 4.2中的form_tag(可能更早一些)有一个:execuce_utf8选项;
If set to false, a hidden input with name utf8 is not output.
如果设置为false,则不输出名称为utf8的隐藏输入。
(Same as https://*.com/a/28201543/430695)
一样(https://*.com/a/28201543/430695)
#4
10
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_cer_workaround)只适用于不符合标准的浏览器(或者任何其他逻辑)的utf8参数。如果您不想用IE工作区来打扰您行为良好的用户,这是很方便的。
#5
5
I made an initializer to remove the param from GET requests. It's obviously a hack.
我创建了一个初始化程序来从GET请求中删除参数。显然这是一个黑客。
This goes in config/initializers/overrides.rb
:
这是在配置/初始化/ overrides.rb:
# Don't let Rails add a utf8=✓ param to GET forms.
# See http://*.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,这样它就不那么麻烦了。
#6
1
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.
并使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.
和调用。在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
#7
0
Try this in your ApplicationController:
在您的应用程序控制器中尝试:
def default_url_options(options={})
options.delete('utf8')
end
#1
24
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在这里有更多信息:
What is the _snowman param in Ruby on Rails 3 forms for?
Ruby on Rails 3中的_snowman param用于什么?
#2
39
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_tag
helper:
一旦您理解了Rails UTF-8 param的用途,并且出于某种原因您仍然需要删除它,那么解决方案比您认为的要简单……不要使用form_tag助手:
<form action="<%= search_path %>" method="get">
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "search", :name => nil %>
</form>
#3
30
form_tag
in Rails 4.2 (and probably earlier) has a :enforce_utf8
option;
Rails 4.2中的form_tag(可能更早一些)有一个:execuce_utf8选项;
If set to false, a hidden input with name utf8 is not output.
如果设置为false,则不输出名称为utf8的隐藏输入。
(Same as https://*.com/a/28201543/430695)
一样(https://*.com/a/28201543/430695)
#4
10
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_cer_workaround)只适用于不符合标准的浏览器(或者任何其他逻辑)的utf8参数。如果您不想用IE工作区来打扰您行为良好的用户,这是很方便的。
#5
5
I made an initializer to remove the param from GET requests. It's obviously a hack.
我创建了一个初始化程序来从GET请求中删除参数。显然这是一个黑客。
This goes in config/initializers/overrides.rb
:
这是在配置/初始化/ overrides.rb:
# Don't let Rails add a utf8=✓ param to GET forms.
# See http://*.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,这样它就不那么麻烦了。
#6
1
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.
并使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.
和调用。在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
#7
0
Try this in your ApplicationController:
在您的应用程序控制器中尝试:
def default_url_options(options={})
options.delete('utf8')
end