I tried
@posts = Post.page(params[:page]).per_page(10)
and
@posts = Post.paginate(:page => 1, :per_page => 10)
You should use Kaminari https://github.com/amatsuda/kaminari
Use the following gem.
Very helpful.
https://github.com/lucasas/will_paginate_mongoid
Posting several years later, in case someone else faces the same challenge.
kaminari-mongoid
was released in 2016, and is currently maintained.
https://github.com/kaminari/kaminari-mongoid
All the goodness of Kaminiari for Mongoid, including appropriate handling of Mongoid::Criteria
results, which was possibly the cause of the OP's error.
silly thing, but it worked for me in sinatra after i added require 'mongoid-pagination' to app.rb
To update a bit the answers, now exists the Pagy gem, also much more performant (cpu/mem) than will_paginate and kaminari. This is a migration guide
Still using will_paginate is also okay.
This thread has same issue: undefined method `paginate' for Array on Rails 3 with mongoid
The main point to fix the error is to add this line before the controller call paginate library:
require 'will_paginate/array'
It should be added to default config file if you use mongoid for the whole project.
Hope the explanation helpful.
Reference from origin gem source: https://github.com/mislav/will_paginate/wiki/Backwards-incompatibility at "WillPaginate::Collection" section.
P/S: this is just a work around if your query is not very large. If you want better performance, let's try mongoid-pagination gem, custom will_pagination gem or another pagination gem which supported Mongoid like kaminari.