Hands On Sinatra Intro

Hands On Demo

This introductory demo script uses source code from sinatra_first_project, which you can find in my sinatra_examples repo. You should copy it step by step if you really want to learn!

Steps

  • Ruby 3.0.3
  • Directories
    • Create directory myapp/ in a convenient spot and cd to it
    • Set that directory up as a git repo: git init
  • Make sure you have the Sinatra gem installed
  • Create the simplest possible sinatra app and run it with ruby

require 'sinatra'

get '/' do
  '<h1>Hello world</h1>!'
end
  • Create a gemfile, bundle, and run the program
  • Install Postgres: Detailed Installation Guides
  • Update app.rb to full example

  • Add model directory,
    • And model.rb
  • Create config directory
    • database.yml tells activerecord where and how the databases are
    • Indicate the modes and the database names
  • Add db directory
    • Take a look at schema.rb
    • create migration in migrate folder
    • rake db:migrate
  • Run the program
    • Error, no erb!
  • Create views directory
    • Add index.erb with a form in it.
    • Form will allow user to enter data
    • But entering a new model gives an error
    • Now add a another view called models.erb
  • Show that we have a list of models now
  • Success!

Screencast of Handson Demo