Getting started with Sinatra

When porting this site from Drupal to Sinatra somewhere to host it was one of the first priorities . Thinking about the deployment process is often a good way of planning and learning the overall workflow and how the framework works. For this Heroku was looked at. They offer a free tier which seemed enough for this site's needs and also the documentation is fantastic. Take a look at the Heroku Getting Started page to sign up, install the Heroku toolbelt and login to your Heroku account via the command line terminal.

By cloning the Heroku ruby-sample Sinatra app you can see how an app needs to be structured. It's a very basic 'Hello World!' app, but has a few extra files needed to get things running on Heroku such as the Gemfile, Gemfile.lock and Procfile. To give the site a nicer look bootstrap was added. After creating a 'public' folder within the Sinatra app directory, bootstrap could be downloaded and extracted it there. A 'views' directory can then be ceated, also in the root of the Sinatra app. In the 'view' directory layout.erb was created, which was a basic shell of bootstrap's HTML, and home.erb, which contained the static HTML and content for my homepage. Once this was completed 'Hello World!' in web.rb from the ruby-sample was replaced with a call to my homepage view.

require 'sinatra'

get '/?' do
  erb :home

The same was then done for the 'About', 'Work' and 'Contact' pages, which are all pretty static Sinatra views, wrapped in the layout.erb HTML.

On the 'Contact' page I wanted an email form, this is where I turned to Heroku's Add-on section to look for an email service. I ended up using Sendgrid they too have a free service allowing 200 emails per day to be send. To handle the emails on the Ruby / Sinatra side, I turned to Pony. Here's the code:

require 'sinatra'
require 'pony'

get '/contact/?' do
  @title = 'Contact'
  erb :contact

post '/contact/?' do
  options = {
  :to => '',
  :from => params[:email],
  :subject => params[:name],
  :body => params[:message],
  :via => :smtp,
  :via_options => {
    :address => '',
    :port => '587',
    :domain => '',
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :authentication => :plain,
    :enable_starttls_auto => true
  redirect '/contact'

You'll see I've added "require 'pony'" at the top, this loads the 'pony' gem, this will need installing on your machine using sudo gem install pony and need adding to your gemfile so it gets installed on Heroku. I am now using contact.erb as the view for the contact page and there are environment variables used for the username and password which are stored on Heroku. These can be stored locally to in an .env file in your docroot, just make sure you add this to your .gitignore.

In the next post I will take you through how I setup sinatra-authentication to allow user login, and Datamapper to speak to Heroku Postgres.

Looking for some further help in getting started with Sinatra? Get in touch

UPDATE: Since the article was written, this site has moved from Sinatra to Ruby on Rails. So the article is still valid, but a few of the references to this site may not be correct.

Published on: 29 October 2012
Tags: tutorial, ruby, sinatra,

Simple SEO PDF guide

Get our latest PDF guide, Simple SEO.