While Rails dominates the Ruby-web space with it's patented 'Rails Magic' being seen everywhere from Github to Groupon, Sinatra still plays a vital role for many smaller applications that don't quite need the overhead of a larger, more powerful framework. Since Sinatra is specifically designed to be lightweight and flexible, it's a great framework to use to test out different bits of other frameworks like ActiveRecord or MVC patterns, as it forces you to become intimately familiar with them, by forgoing the hidden magic present in other frameworks.
To get started building our small app, we'll use the Sinatra app generator Corneal.
Generate a New App$ gem install corneal
$ corneal new crud-app
$ cd crud-app
$ bundle install
This may be the most important step in building your app. You need to be sure of all the relations between your models before you begin building them, otherwise, you'll end up spending hours trying to figure out why, for example, a review owns a user instead of the other way around. Additionally, the next two steps are much faster if you've got a complete domain model specced out ahead of time. Here's the model we'll use for this app:
scaffold
$ corneal scaffold new_model
You can also pass additional arguments of the attribute:data_type
$ corneal scaffold movie name:string
$ corneal scaffold user username:string email:string password_digest:string
$ corneal scaffold review content:string rating:integer
This will likely generate more views than we need, (we probably won't want a Users index, for example) but we can clean up these extra files later.
Now that we've got all the files we should need, we can build the models.
With the models created, we can $ rake db:migrate
Since we want to allow users to log in and out of our app, we have to enable sessions in ApplicationController
Each controller you generate will automatically be added to config.ru
Rack::MethodOverride
get
post
RackSessionAccess::Middleware
Now the fun part. If you're taking a TDD approach as I did, this is where you'll want to start writing tests if you haven't already. Since we have all our relationships defined, we now get to create the actual behavior of our application. Start by picking a single action, such as registering a new user, and building out all the endpoints that would need to exist for that action to be completed.
In order for a user to sign up, we'll need:
A simple form can be built in the views/users/signup.html.erb
file (renamed from users/new.html.erb
):
and the routes can be added to the UsersController
:
As you build out your application, you'll find that your routes necessarily become more robust as you develop the need to validate input or redirect users under certain conditions.
From here you should continue choosing actions that are missing from your app and implementing them as needed. In this domain, for example, we'll need to allow users to create, read, update, and delete (CRUD!) reviews, add new movies, and log in and out. After that, we can spend some time designing and developing the front-end, connect our app to a third-party API, or add any number of new features.
Watch the entire development process on YouTube, or view the source on GitHub.
Faking Data - Seeding your database with mock objects|Ruby on Rails Web App pt 1 - Models & Migration