Search code examples
rubysinatrasequelkaminari

Sinatra, Kaminari pagination problems with SEQUEL and postgres


I am attempting to page a dataset with kaminari on Sinatra.

In my Gemfile I have

source "https://rubygems.org"
gem 'sinatra'
gem 'thin'
gem 'pg'
gem 'kaminari', :require => 'kaminari/sinatra'

The Ruby code is:

dataset = DB[:candidates] 
get '/candidate' do

     @items = dataset.order(:id).page(params[:page]).per(5)
     erb :candidate
end

and the error message is:

NoMethodError at /candidate
undefined method `page' for #<Sequel::Postgres::Dataset:0x94a6808>

    file: app.rb
    location: block in <main>
    line: 25

I am stuck. I have also tried will_paginate with similar frustration and failure.

Is the query correct? I have also tried:

@items = dataset.order("id").page(:page=>params[:page]).per(5)

which produces the same error.

Any ideas anybody?


Solution

  • In case someone else comes across this question, you can get pagination working in Sequel as follows without any additional gems:

    Enable the extension:

    DB.extension(:pagination)
    

    Use the pagination extension (as per the question example):

    page = Integer(params[:page]) rescue 1
    @items = dataset.order(:id).paginate(page, 5)
    

    Then its up to your view logic to render page links.