Use Sinatra, Haml, & Postgres to Quickly Build an App & Deploy It to Heroku PART 2

Avatar

Written by Edward Sharp

Software developers monitors and computer using Sinatra, Haml, & Postgres

Here’s part two of an overview guide to using SinatraHaml, and Postgres to quickly build a simple inquiry form web app & deploy it to Heroku. Check out PART 1 first if you haven’t already.

Example source code on Github.

Deploying the app to the internet

You want this to be publicly accessible on the internet? Enter Heroku, the cloud application platform. They provide free, bottom tier, developer accounts– perfect for our response form app sample! The first step is to sign up and install the heroku toolbelt command line tools and get logged in by typing:

heroku login

After that’s done, deploying the app is a breeze.

First, cd to the directory of the app, then type

heroku create

This creates a new heroku app and adds a git remote repo to your project. We must create a few files that Heroku requires. The first is a Gemfile, which contains all the gem dependencies of the app. It looks like this:

source 'https://rubygems.org'
gem 'sinatra'
gem 'sinatra_more/markup_plugin'
gem 'haml'
gem 'dm-core'
gem 'dm-timestamps'
gem 'dm-sqlite-adapter'
gem 'dm-migrations'
gem 'pony'

Gemfiles can be used with bundler, so you could simply add gems to this file and type “bundle” on the command line and bundler would install missing dependencies for you!

The other file we need is named “config.ru” and is used by Heroku to start the app. It will look like this:

require './myapp'
run Sinatra::Application

Next we need to change our app to use Postgres instead of SQLite because Heroku does not support the latter. First, create the free (dev) Postgres DB addon with Heroku, like so:

heroku addons:add heroku-postgresql:dev

Then simply change gem ‘dm-sqlite-adapter’ in the Gemfile to gem ‘dm-postgres-adapter’ as well as changing DataMapper::setup(:default, “sqlite3://#{Dir.pwd}/smc_form.sqlite”) to DataMapper::setup(:default, ENV[‘DATABASE_URL’]) (note, ENV[‘DATABASE_URL’] is a environment variable Heroku sets up so that you don’t have to hardcode the DB user, pass, address, and DB name into your code!)

Check out if your DB was created successfully with the following commands:

heroku pg:info
heroku config

Finally, we need to setup and config outgoing mail. Heroku provides a free tier addon for sendgrid (I had to “verify” my account first). Go ahead and add that with the following command:

heroku addons:add sendgrid:starter

Now add the proper config to myapp.rb (notice those handy environment variables again!)

Pony.options = {
:via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => '587',
:domain => 'heroku.com',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
}
}

We’re nearly finished! Let’s push this app up to Heroku! Simply git push to the heroku remote:

git push heroku master

And test it out! Here’s my instance.

Did something go wrong?

Something not working? Check out the logs:

heroku logs

Or even using tail!

heroku logs -t
Avatar

Written by Edward Sharp

Have a project?

Contact Us