mislav edited this page Aug 10, 2011 · 7 revisions

This is a FAQ on a number of common pitfalls seen on the Google group.

First make sure that you have the latest release of will_paginate compatible with your Rails version. Each new release fixes a number of bugs. Check the installation instructions and see below how to find out which version do you have.

If these tips don’t help and you become sure you’ve found a bug, see Report bugs.

I’m not sure if I installed will_paginate correctly in my Rails application.

Your application should either load the will_paginate gem or have the plugin located in “vendor/plugins/will_paginate/” directory. To see if the library has been loaded, open the console for your app and try the following lines:

>> defined? WillPaginate
>> ActiveRecord::Base.respond_to? :paginate

If any of these lines return nil/false, will_paginate has not properly loaded in your app.

I get “undefined method `paginate’” after upgrading to will_paginate v3.0

It’s possible that you are trying to paginate a static Array instead of performing a paginated query in the database. For instance, chaining a paginate call after an Active Record find or all methods is wrong:

# this is wrong:
Post.find(:conditions => '...').paginate(:page => 1)

The above line will return the desired result but defeats the purpose of pagination. Here, the find query will first load all the records from the database, which is dangerous and should be avoided.

If you know what you’re doing and really need to paginate Arrays, require this feature explicitly:

require 'will_paginate/array'

I don’t know which version of will_paginate I have.

It’s easy to find out the current version. Open the console for your app and type:

>> require "will_paginate/version"
>> WillPaginate::VERSION::STRING

If “will_paginate/version” doesn’t exist, then you have a very old version of will_paginate and you should upgrade.

I’m getting “undefined method `total_pages’” error when rendering in the view!

In newer versions, the `page_count’ method of WillPaginate::Collection class has been renamed to `total_pages’ for consistency. This broke some Sphinx and Ferret plugins for fulltext indexing in Rails because they were emulating WillPaginate::Collection when trying to be compatible.

If you are indeed using 3rd party code like this, please update your Sphinx (or Ferret) plugin to the latest version or ask on their mailing lists about this error. Otherwise feel free to report this on our mailing list.

Not using Sphinx or Ferret but still getting this error? Make sure you’re passing a paginated collection to the will_paginate helper, not an ordinary array.

Pagination works fine until I click on page 2 or further and my URL parameters get lost.

See Simple search for explanation of how this might happen.

Pagination seems to return the same result set regardless of page number.

First inspect the URL. Let’s say that it looks like this:

The default name for the page parameter in the URL is “page”. You have to make sure that, in your controller, you pass that same parameter to the paginating finder:

@posts = Post.paginate :page => params[:page]

If a wrong parameter is passed as :page parameter, pagination will forever be “stuck” on page 1.