Molx
Molx

Reputation: 6931

Remove default 'GET' method parameters from URL

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

Answers (4)

Kombo
Kombo

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

ghaydarov
ghaydarov

Reputation: 11

I solved utf problem by using

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

instead of form_tag, it solved it.

Upvotes: 1

d_rail
d_rail

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

fl00r
fl00r

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

Related Questions