How to cache will paginate (tried with Rails 3)

Posted by Christian-Manuel Butzke on January 25, 2011|Comments

Will Paginate is probably one of the most used plugins out there, so there is no surprise, that
we are using the same.

Now when it comes to caching static html it is important to note, that all the url parameters (the part after the ? in the url),
are ignored. As will paginate is adding by default the page attribute (e.g. http://www.example.com/blogs?page=2) to the url, our
page will not be cached properly.

I searched the internet and found all different kind of solutions, but it comes out, the simplest solution is the best.

We just need to add a route, containing the page attribute in the url to our routes.rb file and we are done. Note, though, and
thats a little gotcha, the entry containing the page parameter needs to come first.

So, thats the solution:

match “blogs/:id/:page”, :to => “blogs#show”, :via => :get match “blogs/:id”, :to => “blogs#show”, :via => :get

Hope, this helps
Cheers

blog comments powered by Disqus