This is the start of a multi-part series of Sinatra introductory lectures. While Sinatra is far simpler than Rails, and easier to understand, it still is a new framework. These series will introduce you step by step to Sinatra and in particular the aspects that are related to Scaling.
Don’t worry if you don’t have precisely the same version number. Ask if you’re not sure
rbenv -v; ruby -v; git --version; psql --version
rbenv 1.2.0
ruby 3.0.3
git version 2.28.0
psql (PostgreSQL) 13.3
sinatra (2.1.0)
sinatra-activerecord (2.0.25)
/config
database.yml
/db
/migrate
...migration files
schema.rb (generated)
/views
...erb files
/test
... test files
/models
... activerecord model files
config.ru
Gemfile
app.rb
Rakefile
# Gemfile
source "https://rubygems.org"
gem "rake"
gem "sinatra"
gem "activerecord"
gem "sinatra-activerecord"
gem "pg"
gem "rack-test"
# Rakefile
require './app'
require 'sinatra/activerecord/rake'
# app.rb
require 'sinatra'
require 'sinatra/activerecord'
require_relative 'models/user.rb'
get '/' do
"Hello Sinatra!"
end
# config.ru
require './app'
run Sinatra::Application
# database.yml, where we declare various DB
# settings and parameters
development:
adapter: postgresql
encoding: unicode
database: sin_series_dev
pool: 2
test:
adapter: postgresql
encoding: unicode
database: sin_series_test
pool: 2
production:
adapter: postgresql
encoding: unicode
database: sin_series_prod
pool: 2
username: your_username
password: your_password
gem rack-test
to Gemfile, bundle
hello sinatra
when it receives a GET
at root
# test/root_test.rb
ENV['APP_ENV'] = 'test'
require_relative '../app.rb'
require 'minitest/autorun'
require 'rack/test'
include Rack::Test::Methods
def app
Sinatra::Application
end
describe 'The HelloWorld App' do
it "says hello" do
get '/'
last_response.ok?
assert_equal last_response.body, 'Hello Sinatra!'
end
end
ruby test/root_test.rb
) it will run the test and it should succeed