4

I'm building an app on Rails 3 RC. I understand the point behind the _snowman param (http://railssnowman.info/)...however, I have a search form which makes a GET request to the index. Therefore, submitting the form is creating the following query string:

?_snowman=☃&search=Box

I don't know that supporting UTF encoding is as important as a clean query string for this particular form. (Perhaps I'm just too much of a perfectionist...hehe) Is there some way to remove the _snowman param for just this form? I'd rather not convert the form to a POST request to hide the snowman, but I'd also prefer it not be in my query string. Any thoughts?

skaffman
  • 398,947
  • 96
  • 818
  • 769
TheOddLinguist
  • 677
  • 2
  • 6
  • 14

3 Answers3

1

You can avoid the snowman (now a checkmark) in Rails 3 by.... not using Rails for the search form. Instead of using form_tag, write your own as outlined in: Rails 3 UTF-8 query string showing up in URL?

Rails helpers are great unless they're not helping. Do-it-yourself is good as long as you understand the consequences, and are willing to maintain it in the future.

Community
  • 1
  • 1
slothbear
  • 2,016
  • 3
  • 21
  • 38
0

In Rails 4.1 you can use the option :enforce_utf8 => false to disable utf8 input tag.

However I want to use this in Rails 3, so I monkey-patched my Rails. I put the following in the config/initializers directory.

# allow removing utf8 using enforce_utf8, remove after Rails 4.1
module ActionView
  module Helpers
    module FormTagHelper
      def extra_tags_for_form(html_options)
        authenticity_token = html_options.delete("authenticity_token")
        method = html_options.delete("method").to_s

        method_tag = case method
          when /^get$/i # must be case-insensitive, but can't use downcase as might be nil
            html_options["method"] = "get"
            ''
          when /^post$/i, "", nil
            html_options["method"] = "post"
            token_tag(authenticity_token)
          else
            html_options["method"] = "post"
            tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag(authenticity_token)
        end

        enforce_utf8 = html_options.delete("enforce_utf8") { true }
        tags = (enforce_utf8 ? utf8_enforcer_tag : ''.html_safe) << method_tag
        content_tag(:div, tags, :style => 'margin:0;padding:0;display:inline')
      end
    end
  end
end
lulalala
  • 17,572
  • 15
  • 110
  • 169
0

I believe the snowman has to be sent over the wire to ensure your data is being encoded properly, which means you can't really remove the snowman input from forms. Since, it's being sent in your GET request, it will have to be appended to the URL.

I suppose you could write some javascript to clean up the URL once the search page loads, or you could setup a redirect to the equivalent URL minus the snowman. Both options don't really feel right to me.

Also, it doesn't seem there is any way to configure Rails to not output it. If you really wanted to get rid of it, you could comment out those lines in Rails' source (the committed patches at the bottom of railssnowman.info should lead you to the files and line numbers). This adds some maintenance chores for you when you upgrade Rails. Perhaps you can submit a patch to be able to turn this off?

EDIT: Looks like they just switched it to what looks like a checkmark instead of a snowman.

EDIT: Oops, back to a snowman.

Jon Smock
  • 9,451
  • 10
  • 46
  • 49
  • Yeah, I agree. I do understand why it's there...and I think it's a great idea to solve the problem at hand. I'd just like the option to forego to IE charset fix in the interest of a clean query. Thanks for your input. – TheOddLinguist Aug 10 '10 at 19:03
  • Yeah, it does irritate me, too. ?search=term looks a lot more clean than ?_snowman=☃&search=term – Jon Smock Aug 10 '10 at 19:06