Michael R. Cook Ruby and Golang Developer

Tag: Tutorials

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.


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.


Up and Going in Roda: A Simple Ruby Blog

Post Image

In my last article we built a simple Roda website, which let us deliver some rather static pages. We’ll build on that work by implementing some basic blogging features such as User accounts and Posts.

Note: as this article was posted in April 2015 I can not guarantee it will still work with the latest versions Roda.

We’ll be using a PostgreSQL database, the defacto DB of many Rubyist, which we’ll interact with using Sequel, an excellent ORM by Jeremy Evans, who is also the developer behind Roda.

Please make sure to install PostgreSQL before continuing. Once that’s done we need to add the appropriate RubyGems to our Gemfile;

# ./Gemfile

gem "pg", "~> 0.18.1"
gem "sequel", "~> 4.21.0"

After running bundle install we need to connect Sequel to the database. In a real world application we’d want to keep our myapp.rb uncluttered by placing the configuration in a separate database.yml file, and ORM initialization in database.rb, but I’ll simplify that for this tutorial by including everything right in the main app file.

# ./myapp.rb

require "roda"
require "sequel"

database = "myapp_development"
user     = ENV["PGUSER"]
password = ENV["PGPASSWORD"]
DB = Sequel.connect(adapter: "postgres", database: database, host: "", user: user, password: password)

class Myapp < Roda
  # ...

It can be useful to use environment variables for things like usernames and passwords, which is what I’ve done here, but if you’d like, just replace the ENV directly with your database user/password credentials. If your host setting is different then make sure to change that too.


Padrino Blog Tutorial using Postgres & DataMapper

Post Image

Recently I’ve been wanting to experiment building websites with Ruby, but without using Rails. I’ve built a few small Sinatra applications, but I really miss having those basic MVC tools that Rails provides. It’s amazing how much easier things are when you have some nice routing and controller helpers. This led me to trying out Padrino, and I have to say it’s looking quite interesting.

Although the Padrino website does have a build-your-own-blog tutorial, it is using ActiveRecord and the HAML view template language. It’s not that I dislike either of these, but I did want try some other tools, such as DataMapper and straight up ERB for the views. As I couldn’t find any good tutorials out there I decided to use that Padrino tutorial as inspiration for my own post. Hopefully you will find it useful.

I will be borrowing heavily from the original Blog Tutorial, so if you’re happy enough using ActiveRecord and HAML, then please do go read that.

Okay, enough of the introductions, let’s get stuck in.


Up and Going in Roda: Static Ruby Websites

Post Image

Recently I started experimenting with the Roda framework to build web applications but as there are not many tutorials, or blog posts in general, about Roda, I thought I’d share my experiences here. Over the coming weeks I’ll be writing a number of articles on using Roda but rather than having to duplicate lots of setup code I think it’ll be easier to have a base app to work from. So, in the next two articles I’ll be building an extremely simple blog using Roda, Postgres, and the Sequel ORM. For this first part we’ll deliver static content, leaving User accounts and posts for part two.

For those who want to follow along, my system is Mac OSX 10.9, along with the following software versions;

  • Ruby 2.2.2
  • Postgres 9.4
  • Roda 2.2.0
  • Sequel 4.21.0

Your application may not work if you use a different version of Roda than what I specify here, but for the rest, you shouldn’t encounter any issues if your versions are a little older/newer.

Installation of Ruby and Postgres is left as an exercise for the reader.

Installing Roda

This is actually the simplest part of the whole procedure as Roda is just a regular GEM. We are however going to be using Bundler for our gem management so first you’ll want to install that, followed by Roda;

$ gem install bundler
$ gem install roda

Creating a New App

Unfortunately there are currently no site generators in Roda like there is in Ruby on Rails (rails new myapp). This means we’ll have to generate our application directory and file structure by hand. Here’s the outline that we’ll be using for our simple blog;

- myapp/
  - models/
  - public/
  - views/
  - config.ru
  - Gemfile
  - myapp.rb
  - models.rb

Go ahead, create the base app directory;

$ mkdir ~/myapp
$ cd ~/myapp

I always like to use Bundler for the gem management so we’ll need to create a Gemfile in which we’ll specify the basic gems requirements for delivering static content;

# ./Gemfile

source "https://rubygems.org"

gem "roda", "~> 2.1.0"
gem "tilt", "~> 2.0.1"
gem "erubis", "~> 2.7.0"

Remember to run bundle install now, and each time you make a change to this file.

At present we have just three dependencies; the first is Roda itself, but we’re also including tilt and erubis so that we can use ERB templates in all our views.

Like all Ruby web frameworks, Roda is built on top of Rack, so we’re going to need a config.ru so that when we start the server (with rackup), our application will be loaded;


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.