user1903663
user1903663

Reputation: 1725

will_paginate and SEQUEL paging results

I am using sinatra, sequql and postgresql.

So far, I have:

require 'will_paginate'
require 'will_paginate/sequel'
require 'will_paginate/collection'
require 'will_paginate/version'
require 'sequel/extensions/pagination'

The ruby code is:

get '/candidate' do
     @items = DB[:candidates].order(:id).extension(:pagination).paginate(1, 10)
     erb :candidate
end

In the view:

<%= will_paginate @items %>

I have tried multiple variations on the query to get paging to work, all ending in failure. The following generates a page of 10 rows, as requested, but then returns an error message when I click page 2.

I have tried with and without the sequel extension pagination included and the creator of "will_paginate" is adamant that they can work together anyway.

Furthermore, on the suggestion of others, I have tried:

get '/candidate' do
  @items = DB[:candidates].order(:id).paginate(:page => params["page"], :per_page => 10)
  erb :candidate
end

and

get '/candidate' do
  page = params.fetch "page", 1
  per_page = params.fetch "per_page", 10
  @items = DB[:candidates].order(:id).paginate(:page => page, :per_page => per_page)
  erb :candidate
end

neither works. I get an error which basically says "wrong number of arguments (1 for 2)".

Does anybody have experience of successfully paging results with SEQUEL? All help gratefully received.

Upvotes: 1

Views: 851

Answers (1)

Fletcher Moore
Fletcher Moore

Reputation: 13814

I had the same problem. The paginate method does not take :page and :per_page parameters. https://github.com/jeremyevans/sequel/blob/master/lib/sequel/extensions/pagination.rb

For some reason fetch would not work for me either. I ended up using:

page = params[:page].to_i
page = 1 unless page > 0
@notes = DB[:notes].extension(:pagination).paginate(page, 20)

Upvotes: 1

Related Questions