# Filtering-sorting form

**URL:** <https://www.ruby-forum.com/t/filtering-sorting-form/168920>\
**Category:** Rails\
**Created:** [May 27, 2009, 11:41am UTC](https://www.ruby-forum.com/t/filtering-sorting-form/168920 "2009-05-27T11:41:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fRAnKEnSTEin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/0ea827/32.png) [@fRAnKEnSTEin](https://www.ruby-forum.com/u/fRAnKEnSTEin)\
**Post date:** [May 27, 2009, 11:41am UTC](https://www.ruby-forum.com/t/filtering-sorting-form/168920/1 "2009-05-27T11:41:52Z")

</div>

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=name)  
[http://www.something.com/gallery/page/4?orderby=category\_id](http://www.something.com/gallery/page/4?orderby=category_id)  
[http://www.something.com/gallery/page/4?orderby=price](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?category=1)  
[http://www.something.com/gallery/page/4?ocation=3](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](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=](http://www.something.com/gallery/page/4?category=&orderby=) (in this  
case i set defaults values for each parameter)

Any idea, suggestion?

Regards

---

<div class="post-metadata">

**Author:** ![fRAnKEnSTEin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/0ea827/32.png) [@fRAnKEnSTEin](https://www.ruby-forum.com/u/fRAnKEnSTEin)\
**Post date:** [May 27, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/filtering-sorting-form/168920/2 "2009-05-27T12:06:27Z")

</div>

On 27/05/2009, at 7:40 PM, fRAnKEnSTEin wrote:

> link\_to “Category”, :action =\> :index, :orderby =\> “category\_id”
> 
> [http://www.something.com/gallery/page/4?category=1](http://www.something.com/gallery/page/4?category=1)  
> [http://www.something.com/gallery/page/4?ocation=3](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:

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

* * *

Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
Last updated 20-May-09 (Rails, Basic Unix)  
Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)

---

<div class="post-metadata">

**Author:** ![fRAnKEnSTEin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/0ea827/32.png) [@fRAnKEnSTEin](https://www.ruby-forum.com/u/fRAnKEnSTEin)\
**Post date:** [May 28, 2009, 11:26am UTC](https://www.ruby-forum.com/t/filtering-sorting-form/168920/3 "2009-05-28T11:26:49Z")

</div>

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](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](http://www.sommething.com/gallery/page/1/criteria/au/category/1) and so  
on, any idea?

Regards

---

<div class="post-metadata">

**Author:** ![fRAnKEnSTEin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/0ea827/32.png) [@fRAnKEnSTEin](https://www.ruby-forum.com/u/fRAnKEnSTEin)\
**Post date:** [May 28, 2009, 11:24am UTC](https://www.ruby-forum.com/t/filtering-sorting-form/168920/4 "2009-05-28T11:24:28Z")

</div>

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.angelazul.cr/galeria/page/1?type=&criteria=au&category=1&orderby=price&ocation=1](http://www.angelazul.cr/galeria/page/1?type=&criteria=au&category=1&orderby=price&ocation=1)

use friendly urls?, any idea?

Regards
