Sinatra Settings and Configuration


You can set app-level variables that you wish to access throughout your app using the set method:

set :name, 'Mysite'
set :author, 'DAZ'

These can be accessed later using and

get '/hello' do
  "This page was written by " +

Going to ‘/hello’ will produce:

This page was written by DAZ.

You can also use set to change certain settings. One that I like to do is:

set :haml, { :format => :html5 }

This makes haml spit out html5 (correct doctype, no closing slash on images etc).

You can also make changes to Sinatra’s built-in settings. For example, you can change the default public directory like so:

set :public, { root }

This will set the public folder to be the same as the root folder.

You can also change the default folder where template files are stored:

set :views, { File.join(root, "templates") }

Sinatra will now look in the folder called templates for all of the template files.

If a settings is a boolean option, then you can use :enable and :disable instead:

enable :sessions
disable :twitter

Is exactly the same as:

set :sessions, true
set :twitter, false

See more possible settings on the Sinatra homepage

Configuration Blocks

Sinatra has a nice way of setting up basic configuration using configure blocks like so:

 configure do
  set :name, 'daz'
  set :haml, { :format => :html5 }

This will set up any settings as well as anything else that is required initially.

One nice feature is that you can use different configure blocks for each different environment

configure :development do
  set :db, File.join("sqlite3://",settings.root, "development.db"

configure :test do
  set :db, File.join("sqlite3://",settings.root, "test.db"

configure :production do
  File.join("sqlite3://",settings.root, "production.db"
  set :sass, { :style => :compressed }

Notice that I’ve set Sass to output compressed in production, which helps to make the file smaller. There aresass other options for Sass and Haml that can be set in this way.

Environment Settings in Heroku

You can set up environment variables on Heroku to avoid having to make certain settings public. For example you might have this code:

set :password, 't0psecret'

Unfortunately you might also be sharing your code and not want people to have access to your password. The answer is to set an environment variable called PASSWORD. This is very easy to do in the terminal, just navigate to your app’s directory and copy the following code:

$ heroku config:add PASSWORD=t0psecret

You should see the following message:

Adding config vars:
  PASSWORD    => t0psecret
Restarting app...done.

You can check the values of your environment variables using the following command:

$ heroku config

Notice that Heroku sets up some environment configuration variables automatically (these can often come in handy).

PASSWORD => t0psecret
DATABASE_URL => postgres://
RACK_ENV     => production
URL          =>

If you want to get rid of all of your configuration variables, just use the following command.

$ heroku config:clear

This should be confirmed by Heroku, followed by an app restart:

Clearing all config vars and restarting app...done.

Once you have set up all of your environment variables on Heroku you can change your app settings to the following:

set :password, ENV['PASSWORD'] || 'secret'

This will use the Heroku environment variable that is stored as ENV‘PASSWORD’ if one is set, or otherwise just use the dummy password of ‘secret’. This means that you don’t need to show any important settings in your actual code.

blog comments powered by Disqus