Forum: Ruby on Rails filtering-sorting form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
482517e3e7f1be3c858dbdb4b8f288a1?d=identicon&s=25 fRAnKEnSTEin (Guest)
on 2009-05-27 11:41
(Received via mailing list)
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...

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

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

Any idea, suggestion?

Regards
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-05-27 12:06
(Received via mailing list)
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?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/
Last updated 20-May-09 (Rails, Basic Unix)
Blog: http://random8.zenunit.com/
Twitter: http://twitter.com/random8r
482517e3e7f1be3c858dbdb4b8f288a1?d=identicon&s=25 fRAnKEnSTEin (Guest)
on 2009-05-28 11:24
(Received via mailing list)
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=&crite...

use friendly urls?, any idea?

Regards
482517e3e7f1be3c858dbdb4b8f288a1?d=identicon&s=25 fRAnKEnSTEin (Guest)
on 2009-05-28 11:26
(Received via mailing list)
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=&cri......

use friendly urls? maybe something like:

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

Regards
This topic is locked and can not be replied to.