Default folder for partials is views/ and name is with .erb extension. Default layout file is views/layout.erb. Always use symbols when referencing templates, event with subfolder erb :'subdir/home'. Instance variables are accessible in templates. Static files are server from public/ folder. For stylesheet use scss :stylesheet, style: :expanded For coffescript coffee :index

# to change default views folder
set :views, settings.root + '/templates'

Filter can be used to modify request and response, set instance variable.

before '/protected/*' do

Session and other configuration options

require 'securerandom'

enable :sessions
set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex(64) }

# to be able to access from other computers, or run with `ruby app.rb -o
set :bind, ''

get '/:value' do
  session['val'] = params['value']

Redirect with redirect to('/foo?bar=42'). Cache-control header is set with cache_control :public. Rack cache can be used for caching.

Top level application assumes a micro app style configuration (a single app file, public and views folders). If you want modular style (run is disabled by default) you can inherit from Sinatra::Base (logging and inline templates disabled by default). If you want similar to classic style, you can write with:

require 'sinatra/base'

class MyApp < Sinatra::Application
  get '/' do
    'Hello world!'
require './app'
run MyApp

Run with

bundle exec rackup -p- 4567

Sinatra Contrib

Reloading in Sinatra can be done with rerun or with

require 'sinatra/reloader' if development?

Sinatra book


Inside get actions you can define helper and use request logger

get '/' do 'get_root'

For use in templates you can define helpers

helpers do
  def logger

To enable logging to a file use as well to stdout. Problem is that I do not see custom logs with params

configure do
  # logging is enabled by default in classic style applications,
  # so `enable :logging` is not needed
  # create folder `mkdir log` and gitignore with `touch log/.keep`
  file ="#{settings.root}/log/#{settings.environment}.log", 'a+')
  file.sync = true
  use Rack::CommonLogger, file

For use in other places you can create global variable (I do not know how to access logger outside of actions). But the problem here is that I do not see messages in file (but see in STDOUT on developemnt) when I use $ but inside actions works fine.

configure :production do
  $logger ='log/common.log', 'hourly')
  $logger.level = Logger::WARN

  # Spit stdout and stderr to a file during production
  # in case something goes wrong
  $stdout.reopen("log/#{settings.environment}.log", 'w')
  $stdout.sync = true

configure :development do
  $logger =


APP_ENV=production ruby my_app.rb
# or
ruby my_app.rb -e production

Sinatra Sass

To add support to scss you can add gem 'sass' and if using modular style

require 'sass/plugin/rack'

Sass::Plugin.options[:style] = :compressed
use Sass::Plugin::Rack

or is using classis style, run

sass --watch public/assets/stylesheets/sass:public/assets/stylesheets

and include in layout

<link rel="stylesheet" href="/assets/stylesheets/switch.css">


# Gemfile
gem 'sinatra-activerecord'
gem 'sqlite3'
gem 'rake'

# app.rb
require 'sinatra/activerecord'
set :database, adapter: 'sqlite3', database: "#{__dir__}/db/home_automation.sqlite3"

# Rakefile
require 'sinatra/activerecord/rake'

namespace :db do
  task :load_config do
    require './app'

Note that we use __dir__ to get app_file folder so it works fine when we run the app from non root path ruby some/path/to/app.rb

And you can create tables

rake db:create_migration NAME=create_temperatures

and models

class Temperature < ActiveRecord::Base

On raspberri pi you should install sqlite3

sudo apt-get autoremove; sudo apt-get autoclean;sudo apt-get clean
sudo apt-get update; sudo apt-get -y dist-upgrade; sudo apt-get install sqlite3
sudo apt-get install libsqlite3-dev

Sinatra tests


You can access console like rails c with


You can use javascript with hot reloading using

Examples of opensource apps

  • Nice Sinatra ActiveRecord Backbone app. It