Search code examples
rubysinatrawill-paginatesequelpagination

How to use will_paginate gem with Sinatra and Sequel


I am trying to use the will_paginate gem to paginate my blog posts, i am also using Sinatra and Sequel for the blog. I receive an error stating

"undefined method `paginate'"

And I have tried everything to make it work, but it's always the same error.

page = params.fetch "page", 1
per_page = params.fetch "per_page", 3
@posts = Post.order(:id).paginate(page.to_i,per_page.to_i)

or

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

Both produce the same error no matter which query I provide.

Is there any way to make this work, or is there any other way so I could paginate my posts using Sequel and Sinatra?


Solution

  • Not specifically Sequel-related, but you can paginate any collection (even those will_paginate doesn't integrate with by default) using this:

    require 'will_paginate/collection'
    paged_collection =
      WillPaginate::Collection.create(page, per_page, total_count) do |pager|
        pager.replace(collection)
      end
    

    EDIT:

    Try this:

    require 'will_paginate'
    require 'will_paginate/sequel'
    require 'sequel/extensions/pagination'
    
    page = params.fetch "page", 1
    per_page = params.fetch "per_page", 3
    @posts = Post.order(:id).extension(:pagination).paginate(page.to_i,per_page.to_i)