Removing "utf8=✓" from Rails 3 form submissions

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?


Solution 1:

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)

Solution 2:

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>

Solution 3:

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?

Solution 4:

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.