Phase 2 Project Complete!

Rianna Castro
3 min readJun 15, 2021

For my Phase 2 project at Flatiron School, I was instructed to create a fully functioning web application using Sinatra. I have been enjoying this phase and what I learned, so I couldn’t wait to get started on it. For my project, I created a “Car Inventory” application. A user has many cars, but each car only belongs to one user.

In my application, a user is able to sign up by using an email and creating a password. Once signed up, they are able to view cars in the inventory, add their own cars to the inventory, edit their cars that are listed, and delete their cars. The user who is signed in is only allowed to view the other cars that are not owned by them. The user signed in can only create, update, and delete the cars they own on the web application. (Create, read, update, and delete is also known as CRUD, in case you didn’t know!) Also, only people who have an account or sign up for one are able to view the car inventory.

Now here’s a couple struggles that I ran into while building this application:

1. Keeping my code DRY:
This is SOOO important because throughout my time at Flatiron, I have learned that code can get messy, quick. While working in my CarController, I noticed that I had many repetitions in my routes and it started to get “ugly.” I was constantly writing:

if @car.user != current_user
redirect ‘/cars’
end

So to resolve this, I created a private method in my ApplicationController.

Code Snippet of Private Method in ApplicationController

What does this private method do, you ask?

In the picture above, this is the private method that I used when a user may try to do something they shouldn’t be allowed to do. For example, let’s say that car #1 is not owned by the user that is currently signed in. He/she does not have a visible option to edit car #1. However, this user could try to type in “/cars/1/edit” to get to the edit page. Thanks to this method, if the user tried to do this, he/she would be redirected back to the page with the list of cars.

2. Not showing the user my error messages:
If a user tried going to a URL or a record that did not exist, a page would show up that gives the error message regarding my code. This is a definite no, because I would NEVER want a user to see this page. In order to fix this, I added a couple things in my code:

Added set :show_exceptions, false
added “set :show_exceptions, false”
method if user tries to go to a non-existent URL

I added this method with an erb file called “error.” This is when a user tries to go to a URL that does not exist. In the error.erb file, I added a message for the user that says “This URL doesn’t exist” as well as added a button below to redirect them to the home page.

method if user tries to go to a record that does not exist

I added this in my ApplicationController in case a user tries to go to a record that doesn’t exist. For example, if the user typed in “cars/242213563” (which does not exist), it would redirect the user back to the “/cars” page.

This project got tricky at times but I definitely had a lot of fun creating it! I’m starting to feel more comfortable and confident when coding. I have learned so many new things throughout this phase and I’m excited for what’s to come in the next phase!

--

--