Michael R. Cook Ruby and Golang Developer

Tag: Pagination

Pagination with Roda, Sequel and will_paginate

Post Image

Anyone who’s used Ruby on Rails has likely needed to implement pagination, and for that you probably used WillPaginate. Although it’s been possible to use WillPaginate with Sequel for some time now, we’ve only just seen the first Roda GEM released. After some experiementation, it turns out adding pagination to our Roda app is actually pretty easy.

Requirements

We’ll be building on top of my Roda Blog tutorial so if you don’t already have an application to try this with then you should take a look at that first.

Here’s the versions and GEMs I’ll be using for this tutorial;

  • Roda 2.3
  • PostgreSQL 9.3
  • Sequel 4.23
  • roda-will_paginate 0.0.4

Enabling the plugin

Our first task is too add roda-will_paginate to our Gemfile – don’t forget to bundle install once you’ve done that.

# ./Gemfile

gem "roda-will_paginate", ">= 0.0.4"

The plugin only handles generating the HTML links on your view pages, so we need to make sure our blogs Post collection includes the required WillPaginate methods. As the plugin README states, “That would mean adding current_page, per_page, offset, total_entries, total_pages as methods of your collection and including WillPaginate::CollectionMethods in your collections as well.”

That’s quite a bit of work, but lucky for us we’re using Sequel, and WillPaginate supports that out-of-the-box.

Read more...

About Me

Hi, my name is Michael and this is my personal blog. Here I’ll be posting various coding thoughts and experiments; everything from writing blogs in Ruby, to Go tools and Z80 Assembly. This site is powered by Thunderaxe, a blogging platform I built using the Roda Ruby framework.