Skip to content

Instantly share code, notes, and snippets.

@ChengLong
Last active January 9, 2021 00:09
Show Gist options
  • Save ChengLong/7003881 to your computer and use it in GitHub Desktop.
Save ChengLong/7003881 to your computer and use it in GitHub Desktop.
Use will_paginate with ActiveRecord in Sinatra
  1. Add gem 'will_paginate', '~> 3.0' to Gemfile
  2. Add %w(will_paginate will_paginate/active_record).each {|lib| require lib} to config.ru
  3. Assuming modular Sinatra, open Sinatra::Base and add include WillPaginate::Sinatra::Helpers so that all views can use will_paginate
  4. In controller, @users = User.paginate(:page => params[:page], :per_page => 30)
  5. In view, = will_paginate @users will generate the pagers

If you want to style your pagers

  • Include this css in your layout
  • Style it
.digg_pagination
  = will_paginate @notifications
@doublechiang
Copy link

Another document even original document does not include instruction to add the following command.
include WillPaginate::Sinatra::Helpers
You save me 2 hours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment