I’ve been plugging away at the Learn-Verified program from Flatiron School. Since my last blog post about building a Ruby Gem, I’ve completed the sections for SQL, ORM, and Rack. The current section I’m finishing up is the Sinatra section. Similar to the OO Ruby section, the Sinatra section wraps up with a project where we build something of our own design and will be assessed on it.
For my Sinatra project, I built a simple wedding planning app. I’m in the midst of planning a wedding and wrangling guest lists and vendors has been tough to keep organized.
This app is certainly not an all-encompassing wedding planning app. It’s primary focus is to help someone track their invited guests, RSVPd guests, and vendors.
In order for users to create a wedding in the app, the first thing they’ll need to do is create an account. Sinatra makes it pretty easy to have user accounts and a login system. The login system uses sessions to ensure only approved users can see certain content and perform certain actions. Below is the code the login system:
The helpers section at the end of the ApplicationController has some crucial methods to simplify controlling access. For example, the #login_redirect method handles all the situations when a user tries to access something before logging in or if they try to access content that doesn’t belong to them. The cool thing about having this method is it provides a single point for editing how I want to handle these cases. I have 4 controllers using this #login_redirect method, and it would be highly error prone without a centralized method for handling access control.
Moving on to my models. A user is permitted to have 1 wedding. A wedding has many vendors and guests.
Validations were an important part of this project and ActiveRecord makes it simple to ensure the models have the right data. Check out my models below:
Another cool thing about ActiveRecord is the “dependent: :destroy.” In my Wedding model, I used this function on the guests and vendors so whenever a wedding is deleted, all those guests and vendors are also deleted from the database. Without this, my database would quickly become cluttered with guests and vendors that don’t belong to any wedding.
The next crucial part of this Sinatra app are the controllers for the wedding, guest, and vendor models.
Each of these controllers can handle showing content to the user, adding new content, editing existing content, and deleting content. For the most part, these 3 controllers all have similar code. Check them out below:
To complete this app, I just needed to put together some views to make all this content presentable to a user. Additionally, I added Bootstrap to make it a little more pretty and have a cleaner structure.
All in all this was a fun project that really hammered home HTTP methods and web application structure. I had built some Rails app prior to doing the Learn-Verified course, but there was always a lot of mystery with how things worked. Working through 48 lessons of Sinatra including this custom project has been great for illuminating how the nitty gritty of web applications work.