Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 758
grautur Avatar asked Dec 20 '10 07:12

grautur


3 Answers

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)

like image 200
Paul Annesley Avatar answered Nov 11 '22 06:11

Paul Annesley


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>
like image 22
Andrew Avatar answered Nov 11 '22 06:11

Andrew


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?

like image 26
cdmwebs Avatar answered Nov 11 '22 07:11

cdmwebs