A skeleton web application configured to use Sinatra and ActiveRecord with some simple conventions:
-
viewsdirectory set toapp/views/(current controller name) -
default layout configured to
app/views/layouts/application.erb, with per-controller layoutsapp/views/layouts/(current controller name).erbused first -
database tables using non-auto-incrementing IDs (see
UniqueId)
Clone sinatree:
$ git clone https://github.com/jcs/sinatree.git
$ cd sinatree
Then install Bundler dependencies:
sinatree$ bundle config set --local path vendor/bundle
sinatree$ bundle install
Initialize a session secret key:
sinatree$ ruby -e 'require "securerandom"; print SecureRandom.hex(64)' > config/session_secret
To create a database table users for a new User model:
sinatree$ $EDITOR `bundle exec rake db:create_migration NAME=create_user_model`
class CreateUserModel < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.timestamps
t.string :username
t.string :password_digest
end
end
end
Then run the database migrations:
sinatree$ bundle exec rake db:migrate
The new User model can be created as app/models/user.rb:
class User < DBModel
has_secure_password
end
A root controller can be created as app/controllers/home_controller.rb:
class HomeController < ApplicationController
self.path = :root
get "/" do
"Hello, world"
end
end
To run a web server with your application:
sinatree$ bin/server
To access an IRB console:
sinatree$ bin/console
Copyright (c) 2017-2025 joshua stein <jcs@jcs.org>
Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.