For many reasons, I’m a huge fan of Rails for building web software, and by implication, coding in Ruby. When you’ve spent a little time working with Ruby, it’s difficult to go back to more traditional languages such as PHP.
For less intense website or small applications, though, Rails can feel a bit heavy-handed. Whilst Rails 3 goes a long way to improving this, I’ve recently been trying out the lightweight ruby framework, Sinatra.
Sinatra is a barebones framework for getting web applications up and running quickly. The idea is that Sinatra gives you a base into which you pull together the things you need, effectively building your own mini-framework. The process is enhanced by the vast array of Ruby Gems that are available.
In this post, I’ll show how to build a simple Sinatra application configured with Bundler and running on Rack that lets you quickly start building lightweight web applications.
Creating Your Sinatra Project
Unlike Rails, Sinatra doesn’t come with generators, so you’ll need to create the project layout yourself. Whilst you can use your own folder layout, there are a few conventions for which Sinatra is configured. Something like this should be good to get started:
touch myproject/app.rb myproject/config.ru myproject/Gemfile
Running these commands generates the following folder structure:
If you’ve used Rails 3, or worked with the Bundler gem and Rack, most of this should look familar:
Gemfile is used by Bundler to declare rubygems the project relies on, whilst
config.ru is used by Rack to configure and run the application.
Next, edit the Gemfile to load Sinatra and any dependencies. My markup of choice for HAML for layout (HTML) and SASS for CSS stylesheets. Sinatra makes using HAML (or any other renderer, such as ERB) very simple:
To install the bundle, just run
bundle install from within your project folder:
Configure Sinatra to run on Rack
My development environment is setup to run Ruby projects on Rack using the Passenger gem. To configure Sinatra to run on Rack, our project just needs a
config.ru file. This file tells Rack to load the any required gems and star your Sinatra application:
set :run, false
set :raise_errors, true
Build Your App
With the environment configured, it’s time to build a small app to test everything. Start by adding a root path to
app.rb and a corresponding
index.haml file under
set :haml, :format => :html5
get "/" do
%title My Sinatra App
%h1 Hello World
%p Your app is up and running!
get block routes any HTTP GET requests for the site’s root path (
example.com/) and renders
views/index.haml using the HAML parser.
Running Your Application
To see your application running, start Rack by calling
rackup from your project directory.
Now navigate to localhost:9292 to see your Sinatra app working! All being well, you should be greeted with the following:
Initially I had trouble seeing how Sinatra would be useful given the existence of full-stack frameworks like Rails. However, as I’ve taken the time to learn Sinatra, I can see the benefits of such a lightweight framework when used for certain tasks.
Whilst it shouldn’t be thought of as a replacement for frameworks like Rails, Sinatra is another tool available to Ruby developers looking to quickly build and deploy small websites and applications.
When Should You Use Sinatra?
When you should use Sinatra over a larger framework like Rails is entirely dependent on the requirements of your site. Sinatra is capable of connecting with databases using gems like
DataMapper in the same way Rails is. It’s feasible, then, to build large-scale web applications with Sinatra – but my personal preference would be to stick with Rails.
The rule of thumb I’m using at the moment is that if my app requires a database, I’ll build it in Rails, otherwise I’ll try Sinatra.
With that in mind, I’m now using Sinatra to build and run lightweight websites such as the recently launched portfolio of Mark Stocks. Mark’s site only requires simple server-side processing for handling contact forms – a full-blown Rails build seemed a be too much! Sinatra provided just the right base on which to build Mark’s site, whilst retaining the flexibility that comes with the Ruby ecosystem.
In the next tutorial, I’ll extend the basic framework we’ve started here to include of a number of useful Ruby gems built for Sinatra, and show how to use your own custom helper code. When it’s a little more polished, I’ll also publish my working Sinatra base application on github.
- The Sinatra Website
- The Official Sinatra Book
- Bare Sinatra App For Deploying To Passenger