Rails add a param to the end of a search query
Posted
by bob
on Stack Overflow
See other posts from Stack Overflow
or by bob
Published on 2010-05-03T20:21:36Z
Indexed on
2010/05/03
20:28 UTC
Read the original article
Hit count: 558
Hello, I am trying to implement a sort on the returned results of a search with rails.
Filter by: 'recent' %> | 'lowest_price' %> | 'highest_price' %>
here are my links. I would like to add a filter to the end of my search query. So for example, I submit a search and I get back results and in my url I have
http://localhost:3000/junks?search=handbag&condition=&category=&main_submit=Go!
I would then like the user to be able to click one of the above links and have the page come back with a new query (i have this set up in the controller like such)
con_id = params[:condition]
if params[:category].nil? || params[:category].empty?
cat_id = params[:category]
results = Junk.search params[:search], :with => { :category_id => cat_id, :condition_id => con_id }
case params[:filter]
when "lowest_price"
@junks = results.lowest_price.paginate :page => params[:page], :per_page => 12
when "highest_price"
@junks = results.highest_price.paginate :page => params[:page], :per_page => 12
else
@junks = results.paginate :page => params[:page], :per_page => 12
end
else
I would like the user to be able to click on of the above links and append a filter param to the end of the search query so that my controller can pick it up and call the correct database queries as seen in the case statement above.
I'm guessing the url will look like this
http://localhost:3000/junks?search=handbag&condition=&category=&main_submit=Go!&filter="lowest_price"
How can I do this? Is there a better way?
© Stack Overflow or respective owner