Michael R. Cook Ruby and Golang Developer

Tag: Blog

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: "127.0.0.1", user: user, password: password)

class Myapp < Roda
  # ...
end

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.

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.