filtering-sorting form

Hi there,

i have a catalog of products, i am using will paginator for paging the results. now The user can sor the products by: name, price or category, and can filter the products by category and ocation. For doing this i have used link_to hepler like so:

link_to "Name", :action => :index, :orderby => "name"

link_to "Category", :action => :index, :orderby => "category_id"

link_to "Price", :action => :index, :orderby => "price"

for category in @categories    link_to category.name, :action => :index, :category => category.id end

for ocation in @ocations    link_to ocation.name, :action => :index, :ocation => ocation.id end

So, when the user click in the sort functionality, the URL will be any of these:

http://www.something.com/gallery/page/4?orderby=name http://www.something.com/gallery/page/4?orderby=category_id http://www.something.com/gallery/page/4?orderby=price

And when the user click on the filter funtionality the URl will be any:

http://www.something.com/gallery/page/4?category=1 http://www.something.com/gallery/page/4?ocation=3

I am having problems because i do not know how to keep both parameters on the url if the user click on one of the sort links anf one of the ocation or category links. In this case what i want to have in my URL will be:

http://www.something.com/gallery/page/4?category=1&orderby=name

Or if nothing has been selected, i wish for example:

http://www.something.com/gallery/page/4?category=&orderby= (in this case i set defaults values for each parameter)

Any idea, suggestion?

Regards

Hi Frankenstein (;-)),

In your controller, you'll need to do something like this:

@page = params[:page] @category_id = params[:category] @orderby = params[:orderby] @ocation_id = params[:ocation]

and so on.

As you (may or may not) know, these instance variables (the ones with @ signs) get auto-transferred to view for you...

Now in the view, your links will have to include all of these... (omitting them obviously where you're overriding them or setting them specifically - for example where you're setting the sort order link, you wouldn't want to include the sort order instance varaible there...)

eg:

link_to "Name", :action => :index, :orderby => "name", :category => @category_id, :page => @page, :ocation => @ocation_id

for category in @categories    link_to category.name, :action => :index, :category => category.id, :page => @page, :orderby => @orderby, :ocation => @ocation_id end

for ocation in @ocations    link_to ocation.name, :action => :index, :ocation => ocation.id, :category => @category_id, :page => @page, :orderby => @orderb end

And back in the controller, your find command will need a condition clause that only uses the params that have been set, and order clause that only uses the orderby params if it's been set.

HTH, Julian

Hi Julian, Thank you very much, it worked like a charm! :D. Ihave another doubt, is there any way to make this horrible URL:

http://www.sommething.com/gallery/page/1?type=&criteria=au&category=1&o

use friendly urls? maybe something like:

http://www.sommething.com/gallery/page/1/criteria/au/category/1 and so on, any idea?

Regards