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, :action => :index, :category =>

for ocation in @ocations
   link_to, :action => :index, :ocation =>

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

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

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:

Or if nothing has been selected, i wish for example: (in this
case i set defaults values for each parameter)

Any idea, suggestion?


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...)


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

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

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

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.


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

use friendly urls? maybe something like: and so
on, any idea?