rails will_paginate and sorting with page caching
-
22-09-2019 - |
Question
I have a website where a list of products is shown in the category show
action. I successfully paginate with the will_paginate
gem, and make this work with page caching by configuring my routes like Sean Behan does.
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") %>
My Problem is that I pass ?sort_by=name
in the URL, which doesn't work with page caching. Does anyone have an idea how can I do smarter sorting? I would really love to go on and cache pages.
Solution
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