Barefoot Development

classic_pagination is not my friend

I recently upgraded a nearly completed Ruby on Rails project to version 1.2.4, then to version 1.2.5. I worked through the deprecation warnings and everything seemed good to go.

Then, I ran into a snag with pagination -- something I didn't have covered in my functional tests. Because I'm caching this site, I have routes that include the :page property for the paginator in the URL. Otherwise, pages 2-n don't get cached.

After installing the classic_pagination plugin, I'd get an error when viewing pages with pagination code like this in the view:

<%= link_to('« Previous', :page => @article_pages.current.previous) if @article_pages.current.previous %>
<%= link_to('Next »', :page => @article_pages.current.next) if @article_pages.current.next %>

This is the standard, recommended method for creating the Next and Previous links. The error said: undefined method `paginator' for "2":String. The top-most offending line of the stack trace was: vendor/plugins/classic_pagination/lib/pagination.rb:307:in `=='.

The confusing part was that when I removed the :page from the route and added ?page=2 back as a query string parameter, the error went away. In theory, the :page should be the :page whether it is on the query string or embedded in the URL, right?

Well, long story short, here's the fix. Unfortunately, I can't explain exactly why this is necessary, and don't have the time now to dig into the innards of classic_pagination and the supporting Rails infrastructure to find out. I just hope this post helps somebody.

Oh, the fix? Add the "number" method to the previous or next Page object reference, like so:

<%= link_to('« Previous', :page => @article_pages.current.previous.number) if @article_pages.current.previous %>
<%= link_to('Next »', :page => @article_pages.current.next.number) if @article_pages.current.next %>

I'll probably consider classic_pagination my friend again once I get over being grumpy about this ... or, I may consider one of the other pagination contenders for a future application.

Doug Smith, Senior Developer, Barefoot

Attorneys on Rails

Cross-posting this link to our main Barefoot blog, where we describe a significant Rails site we recently completed, and show at a high level how we accomplished it. Let us know if you'd like more details.

Doug Smith, Senior Developer, Barefoot