Fork me on GitHub

RAT PACK

a simple boilerplate for creating production-ready sinatra apps that use activerecord, sqlite, and twitter bootstrap

Get up and Running

  1. Fork and clone this repo.
  2. Run
    bundle install
    .
  3. Start a local server with
    shotgun
    .
  4. Start a console with
    tux
    .
  5. Visit
    localhost:9393
    in your browser.

The Pack

Gemfile

App Structure and Organization

Sinatra apps can be built a bunch of different ways. This is my way. I think it's pretty good.

  • Your app is wrapped in a class called
    App
    that extends
    Sinatra::Application
    . It is wrapped in a module that you should name with your project name.
  • Sinatra runs on Rack! So we have a
    config.ru
    file. All we need to do in there is require our
    app.rb
    file and then run it:
  • To get this running, we'll need to use the
    sinatra
    gem. This, and all of your Gemfiles are required in your app using Bundler at the very top of your
    app.rb
    :
  • Last, but not least, let's configure a
    public
    folder for all our static assets (i.e. things that don't need to be compiled or rendered, like pictures and css.) We'll also define a
    :root
    . This goes inside our
    App
    class definition in the
    app.rb
    file.

The Database!

Hell yes you can use ActiveRecord, even if you aren't in Rails. The structure Rat Pack provides is explained below. If you want more info on how to properly implement RESTful resources with AR in Sinatra, czech out this awesome tutorial.

  • Your sqlite database is made with the code below. Maybe rename
    database.db
    to something more related to your project.
  • All of your models in the
    lib
    directory are required in your app using this code:
  • Since we are using ActiveRecord as our ORM, your models should extend
    ActiveRecord::Base
    like this:

Routes, Helpers, and Views

  • If you're used to Rails, it's worth noting that Sinatra collapses routes controllers into the same blocks that exist inside our
    App
    class in
    app.rb
    . The Pack contains one as an example:
  • Sinatra allows you to use layouts, and by default will use the
    layout.erb
    file in the
    views
    folder. The one the Pack contains is shown below: Note that whatever view you render will be inserted into the layout where
    <%= yield %>
    is.
  • Lastly, Sinatra doesn't come with the ability to support partials out of the box. Therefore, the pack comes with a helper to provide that functionality: Note: this is the absolute simplest way to implement partials in Sinatra. If you want to step it up, or just read more, check out this.

Contact

Ashley Williams
made Rat Pack for her students at the
Flatiron School
.

If you find an issue, please report it on github.