2

I'm using the generic search form, and my url after the search looks like

http://localhost:3000/search?commit=Search&page=2&query=feature&utf8=%E2%9C%93

The search works fine, but I would like to remove the default "utf8=✓" and "commit=Search" parameters from the URL, I'm also using will_paginate and I would like the &page=2 to be after the query parameter leaving it like this:

http://localhost:3000/search?query=feature&page=2

My code:

#posts_controller.rb
def search 
    query = '%'+params[:query]+'%'        
    @posts = Post.find(:all, :conditions => ["content LIKE ? or title LIKE ?", query, query]).paginate(:page => params[:page], :per_page => 5)
end

and

#html form
<%= form_tag(search_path, :method => 'get') do %>
    <%= text_field_tag "query" %>
    <%= submit_tag "Search" %>
<% end %>

and

#routes.rb
match '/search', :to => 'posts#search'

Thanks.

Molx
  • 6,816
  • 2
  • 31
  • 47

4 Answers4

4

See similar questions:

Rails 3 UTF-8 query string showing up in URL?

removing "utf8=✓" from rails 3 form submissions

Basically, to remove 'commit=Search' add :name => nil to the submit_tag. IE needs the utf8 character. However, the second link has a initializer method to remove that part.

In this video, Ryan Bates talks about the name: nil fix (without ajax): http://railscasts.com/episodes/37-simple-search-form

Community
  • 1
  • 1
d_rail
  • 4,109
  • 32
  • 37
1

I solved utf problem by using

<form action="<%= root_path %>" method="get" >
...
</form> 

instead of form_tag, it solved it.

ghaydarov
  • 11
  • 5
1

Ryan Bates did a nice screen cast on exactly what you're trying to do (plus some more).

http://railscasts.com/episodes/240-search-sort-paginate-with-ajax

tokland
  • 66,169
  • 13
  • 144
  • 170
Kombo
  • 2,371
  • 3
  • 34
  • 64
  • Thanks, I was able to remove the "commit=Search" but the utf8=✓ is still there, and I don't want to use AJAX for that. The utf8 param comes from a hidden div created by the form: `
    ` Any ideas?
    – Molx Apr 25 '11 at 20:27
1

You cant just remove it from url as far as YOU send it.

To clean up will_paginate try this

<%= will_paginate @whatever, params => params.merge({:commit => nil, :utf8 => nil}) %>
fl00r
  • 82,987
  • 33
  • 217
  • 237
  • Thanks, this does help with the pagination links, but it's still not perfect, and I can't decide the order of the params. – Molx Apr 25 '11 at 22:02
  • as far as `params` is a hash - so you cant sort it or order it (in Ruby 1.9+ you can). Hash is an unsorted list – fl00r Apr 25 '11 at 22:41