Reputation: 6931
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.
Upvotes: 2
Views: 4974
Reputation: 2381
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
Upvotes: 1
Reputation: 11
I solved utf problem by using
<form action="<%= root_path %>" method="get" >
...
</form>
instead of form_tag, it solved it.
Upvotes: 1
Reputation: 4119
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
Upvotes: 4
Reputation: 83680
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}) %>
Upvotes: 1