Sinatra Project
Building this Sinatra application was probably my favorite project we have done so far although this is only the second one. Module 2 was very educational and taught me everything I needed to know to build this application. Learning ActiveRecord from the ground up really helped me get a grasp of what is really going on behind the scenes in Sinatra.
I knew I wanted to create an app to manage a collection of movies because who doesn’t like movies? I also knew I wanted to have the ability to add reviews to not just my movies but other user’s movies. I started building my app with the corneal gem. This gem makes it incredibly easy to set up your file structure. Corneal has a few commands that will build your models and controllers for you.
After getting the file structure set up I began to set up my models, making sure they had the correct has_many and belongs_to associations. I also made sure my models inherited from ActiveRecord::Base. I then added the has_secure_password and validation macros. I also made sure that my models inherited from ActiveRecord::Base. The following step I took was creating my migrations to create my database. These migrations created my tables based off of my models. Within these tables I added columns to each table that would become the attributes of my models. One very important column that was added to my users table was password_digest. Password_digest works alongside a gem called bcrypt. This gem encrypts your user’s passwords so that data is not exposed to prying eyes. After, I added a seeds file to seed fake data into my database.
I migrated my database and seeded. I then used tux to make sure my associations were correct and they were not. This is where I ran into a problem with my reviews table and could not figure out how to call .reviews on users. Luckily I was able to get help to fix this relationship. After getting that fixed, I started working on my controllers. I made sure my ApplicationController inherited from Sinatra::Base and that UserController and ReviewController inherited from ApplicationController. For user authentication I had to enable sessions and set my session secret inside of my configure block in the ApplicationController. I made sure my application was able to use all of these controllers by adding the use and run keywords in my config.ru file. I also needed to add Rack::MethodOverride for my edit and delete functionalities. This allows a form with a POST request to use the value of a hidden input field as it’s method (e.g. PATCH and DELETE).
After setting up and making sure all of my routes worked correctly, I was then able to start working on my views. I thoroughly enjoyed working on my views because of the ability to embed Ruby inside of HTML. Setting instance variables in your corresponding routes allows you to gain access to it in your views. This is how I was able to list a collection of movies and reviews. For example, creating an instance variable (@movies = Movie.all) would allow me to use @movies in my view and iterate over the array and manipulate each object in that array.
I created multiple forms for creating, updating, and deleting to meet the CRUD requirements. This part was pretty easy. With the help of Pry you can look at the params the form is sending to the route. You can then use the params to pass in and create or update the attributes of an object.
When creating a new review for a movie it was a challenge trying to figure how to automatically associate it with a specific movie. Through trial and error I was able to achieve this. After I established all of my views then I moved onto the CSS aspect of the project. This was probably my favorite part. I have dabbled with CSS for quite a while now. I would not call myself an expert in it but I definitely know quite a bit.
This project definitely proved to be challenging and I am very proud of what I achieved. I learned a lot of useful information through this mod and project.