views:

490

answers:

1

Hi!

I have a website where in the category 'show' action a list of products is shown. I successfully paginate with the will_paginate plugin and made this work with page caching by configuring my routes like Sean Behan does: http://seanbehan.com/ruby-on-rails/how-to-use-pretty-urls-with-rails-will_paginate-plugin/.

But I would love to have users sort products. Currently I have a find in my controller like this:

if params[:sort_by] == "name_desc"
             #@products = Product.find_with_index("%#{params[:search]}%", :order => 'productname desc').paginate :per_page => 15, :page => params[:page]
            @products = Product.find_with_index('params[:search]')
else
...
end

And a simple link in my view:

<%= link_to image_tag("down.gif"), category_path(:sort_by=>"name") %> name<%= link_to image_tag("up.gif"), category_path(:sort_by=>"name_desc") %> 

Not too elegant, uhh?

My Problem is, that way I pass a '?sort_by=name' in the URL which doesn't work with page caching.

Does anyone have an idea how I can do smarter sorting. I would really love to go on and cache pages.

Thanks!

Val

A: 

OK, I found a way to get the best caching possible with my setup:

I still use this solution for the pagination to work (as I get prettier URLs with it). But I had to switch to action caching to allow for passing sorting-params in the URL. Check out this explanation if you are interested: http://cobaltedge.com/rails-action-caching-with-query-parameters

Cheers,

Val

val_to_many