0

I have this search controller url:

http://localhost:3000/search/index?utf8=✓&search=Search_terms

how can trim this to:

http://localhost:3000/search=query

the search code:

#search-box
  #search-form
    = form_tag search_index_path, :method => 'get'  do
      = text_field_tag :search, params[:search], :autocomplete => "off"
      %button#search-button{:type => "submit"}
        %span Go!

Or is that possible??

Please advise.

The questioner
  • 724
  • 10
  • 21

3 Answers3

2

This is not possible by Ruby (or Rails) itself. You need to

  1. append a JavaScript callback to the submit event of the form
  2. validate the form
  3. replace the target URL of the form with the one composed by search= + input value.

Keep in mind that not all characters can be part of an URL. This is the reason why you should pass the search query as parameter, not as part of the URI.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
1

If you don't want to show the the params[search] on the URL, you should change the method from GET to POST in your form.

Read it: http://www.cs.tut.fi/~jkorpela/forms/methods.html

or include your form code on the question...

gabrielhilal
  • 10,660
  • 6
  • 54
  • 81
0

It's totally possible with (just) Rails.

Make sure the page you're submitting to is your root path

(in config/routes.rb)

root :to => "search#index"

Then just ensure your form is posting to root_path

Getting rid of the "snowman" is a little trickier. If you're sure that you won't have anyone submitting non-UTF8 characters then avoiding the form_tag helper and hand-coding your form (easy with such a short form) will get rid of it.

mylescarrick
  • 1,680
  • 8
  • 10
  • According to the original example, the user wants the query in the URL (and not as a param). This is **not** possible with (just) Rails. – Simone Carletti Jul 09 '12 at 11:36
  • It won't be as a named param. It's just going to be sent as key=value with all params requested on that page - hence the root_path has no additional `named` params (since it's the index method on a RESTful controller) – mylescarrick Jul 09 '12 at 11:41