allanberry
allanberry

Reputation: 7775

Kaminari pager not working with Sinatra and Mongoid?

Can't get Kaminari to work with Sinatra and Mongoid. I'm getting this error:

NoMethodError at /api/events
undefined method `page' for #<Mongoid::Criteria:0x007fccb7828c38>

Here is minimal code to get the error:

Gemfile

source "https://rubygems.org"
gem 'mongoid'
gem 'sinatra'
gem 'kaminari-mongoid'
gem 'kaminari-sinatra'

server.rb

require 'mongoid'
require 'sinatra'

class Event
  include Mongoid::Document
end

get '/events' do
  Event.desc(:id).page(params[:page]).per(10)
end

I have tried require 'kaminari', require 'kaminari-sinatra', require 'kaminari-mongoid', all to no avail (I get LoadErrors). I've also tried register Kaminari::Helpers::SinatraHelpers as mentioned here, which also failed.

I've followed the instructions in detail, and have scoured Google and StackOverflow to no avail. This answer didn't work. I can't help thinking I'm missing something easy; I'm not a Ruby veteran. My hunch is it's something with Bundler. Any idea?

Upvotes: 0

Views: 237

Answers (1)

Melina Delgado
Melina Delgado

Reputation: 26

I ran into the problem as well. Unfortunately, kaminari-mongoid has a rails dependency (you can look in the gemspec file here: https://github.com/kaminari/kaminari-mongoid/blob/master/kaminari-mongoid.gemspec). Therefore, it is not possible to use both kaminari-sinatra and kaminari-mongoid.

This solved my problem. https://github.com/ajsharp/mongoid-pagination. Add it to your Gemfile and install with Bundler.

In your app.rb file, require 'mongoid-pagination'

Upvotes: 1

Related Questions