The Main File (app.rb)

app.rb is the main entry point into our app where we define what the app does. Notice how we’ve subclassed Sinatra::Base to make the app modular.

As you can see below, we include some settings for fetching folders as well as defining the public folder (for storing static files). Another important note here is that we register the Sinatra::ActiveRecordExtension which lets us work with ActiveRecord as the ORM.

ruby

# app.rb # Include all the gems listed in Gemfile require 'bundler' Bundler.require module LivingCostCalc class App < Sinatra::Base # global settings configure do set :root, File.dirname(__FILE__) set :public_folder, 'public' register Sinatra::ActiveRecordExtension end # development settings configure :development do # this allows us to refresh the app on the browser without needing to restart the web server register Sinatra::Reloader end end end

Then we define the routes we need:

  • The root, which is just a simple landing page.
  • A “Start here” page with a form where a user inputs the necessary information.
  • A results page.

ruby

# app.rb class App < Sinatra::Base ... # root route get '/' do erb :index end # start here (where the user enters their info) get '/start' do erb :start end # results get '/results' do erb :results end ... end

You might notice that each route includes the line erb :<route>, which is how you tell Sinatra the respective view file to render from the “views” folder.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *