I have a simple search form in my Rails 3 app:
<%= 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: ✓).
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.
How do I do this?
form_tag
in Rails 4.2 (and probably earlier) has a :enforce_utf8
option;
If set to false, a hidden input with name utf8 is not output.
(Same as https://stackoverflow.com/a/28201543/430695)
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:
<form action="<%= search_path %>" method="get">
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "search", :name => nil %>
</form>
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:
What is the _snowman param in Ruby on Rails 3 forms for?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With