DEV Community

CraigSmith789
CraigSmith789

Posted on

Setlist – A Sinatra Project

Just like any project, the first step was to come up with an idea. I needed one that would use a has-many relationship. I decided on an application that would allow musicians to post notes about different songs. They would have the ability to store their own notes, as well as being able to view notes posted by other users. I felt that a user would need the ability to list the Artist, Title, Difficulty level, and notes for each song. Here is an example:
Setlist blog

The project would be built using Sinatra. Sinatra is a Domain Specific Language (DSL) for creating web applications in Ruby. It is built on top of the Rack webserver interface. Getting started was simplified by using the Corneal gem. Corneal provides a scaffold file directory structured to the MVC (Models, Views, Controllers) architecture. A former Flatiron student created the Corneal gem.

With the file structure completed, the next step was getting the back-end set up to receive and store data. I was able to simplify this process by requiring the Sinatra Active Record gem. Active Record adds additional methods and Rake tasks for interacting with my SQLite database using Active Record ORM (Object Relational Mapping). This allowed me to create the database tables using migration files. I would be working with 2 models, Users, and Posts, so I created a table for each with the appropriate columns. Then I created a User and Post model and set the relationships so a User can have many Posts and a Post belongs to one User.

Now it was time to start adding some functionality. I started by adding three controller files Application, Users, and Posts. These were then also added to the config.ru file. Next, I began adding RESTful routes to the controllers. A RESTful route is a route that provides the mapping from HTTP verbs (get, post, put, delete, patch) to controller CRUD actions (create, read, update, delete). Instead of relying solely on the URL to indicate what site to visit, a RESTful route depends on the HTTP verb and the URL.

The first thing that I needed was the ability to have new users sign up and allow returning users to log in. Since I would be storing passwords in the database, I installed the BCrypt gem. BCrypt will store a salted, hashed version of passwords in the database in a column called password_digest.

To create the views to be displayed by the routes, Sinatra uses a templating engine called ERB or Embedded Ruby. ERB files give you the ability to use both HTML and Ruby code together for your views. After completing the Users routes, I moved on to the Posts controller and created the CRUD routes to create and edit posts. Once this was functioning, it was time to add some additional security to the routes. I made sure that a user could only edit their own posts and added some protection to any routes that accessed the database by using some Helper methods. I also added validations to any user inputs.

I created a few users with several posts, and after a few tweaks, I had a completed project. This was my first Sinatra project created from scratch, and it proved to be a great learning experience.

Top comments (0)