33

I have a search query form on my home page (/)

<% form_tag(search_path, :method => :get) do %>
  <%= text_field_tag 'query' %>
  <%= submit_tag "Search", :name => nil%>

<% end %>

When I submit this search form, I want to end up at (/search?query=foo). However, right now I get /search?utf8=%E2%9C%93&query=foo. What's that utf8 parameter doing there? How can I get rid of it?

Thanks.

4 Answers 4

46

The utf8 parameter (formerly known as snowman) is a Rails 3 workaround for an Internet Explorer bug.

The short answer is that Internet Explorer ignores POST data UTF8 encoding unless at least one UTF8 char is included in the POST data. For this reason, Rails injects an UTF8 character in the form to force IE to treat everything as UTF8 encoded.

31

This parameter is a new feature of rails 3.

It was previously the snowman.

It helps IE to really use utf-8.

Avoid using form_tag and it works:

<form action="<%= search_path %>" method="get" >
  <%= text_field_tag 'query' %>
  <%= submit_tag "Search", :name => nil%>
</form> 
3
  • Just what I was after - my search forms aren't getting persisted, so I just want a GET request with no "tick". Snowman could of stayed though, but he's gone now :( May 5, 2011 at 3:23
  • +1 to avoid having the UTF8 param passed on submit. In my case, I don't believe the UTF8 was important.
    – Kevin Zych
    Sep 21, 2014 at 3:53
  • 1
    See my years-late answer re. form_tag's :enforce_utf8 option. Jan 28, 2015 at 20:16
13

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.

2

I think everyone did a great job explaining why it exists, but I feel like the question wasn't answered, which was:

How can I get rid of it?

So everything said is correct, you do need it present to help with "IE" but the reason why it shows up in your address bar is because you are doing a "GET" as opposed to a "POST". Make it a post and it's gone, but then you are also not following convention.

It comes down to which of these are most acceptable to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.