Round Two! — Sinatra Project

Casey Croteau
4 min readJun 22, 2021

It is here. My 2nd project with FlatIron. Full of nerves but anxious to get this right. But to celebrate, YAY I MADE IT TO ROUND 2! Haha! Compared to the first project this was a little bit tougher to make but you know what? I got this! For this to work I need to build an app that someone can use to keep track of the current books of the month. The user can either create/read/update/delete a book on the app. Simple right?

Now Let Me Try To Tell You How I Created This App

Step 1 — Creating Migration files & Database Tables

Once I got my file up and loaded on VStudio, I needed to go through and figure out what files and folders I was going to need in order for my app to work. Next step was to add my migration files. In order to do so I needed to run rake db:migrate and then to create the files in rake db: migrate I had to run rake db:create_migration NAME=create_books. Of course running it a few more times to add the other necessary files I needed for my app. Now in order for my files to inherit from ActiveRecord I would need to create my tables. For books_of_the_month I would need a table for books, genres, authors and users.

Now where does my app get its information from? That is where rake db:seed comes along. Seed.rb is a file where seed data is stored. In this case the title, author, description, popular, book_month and genre to be stored. In order for that to work we would use the find_or_create_by method for book.

Step 2 — Creating App Controllers

Now with creating App Controllers I almost got ahead of myself and did to much that I wasn’t able to do. Thankfully I figured out what I truly just wanted for controllers. I wanted an books_controller, and the user_controller. In each of these files/classes it tells the controllers to handle the incoming requests, responses, and routing.

CRUD (Create, Read, Update, Delete)

Now what is responsible for performing CRUD actions? In this case the main ones geared towards it would be the books_controller and user_controller. Both of them take from the application controller. But what is the purpose of the application controller? It is there to inherit from Sinatra and have access to GET/POST methods. For this to work you also have to add it to your config.ru file. Which is something I nearly missed and got stuck on. Whoopsies!

Step 3 — The View(s)

Views is how in our controller files we can render/test our routes with Sinatra’s ERB extension file. This is where you can write HTML code, your headers and what you want on your page including buttons. Now in order to know if my page is working I create a header in my ‘views/users/login’ and then ran shotgun. Shotgun throughout this whole process was my bestfriend. It was a great way of knowing my app was working or not. In order to figure out what View files I needed I checked back at each of my controllers and what ERB’s they had. Whether it be connected to ‘index’, ‘show’, ‘new’, and ‘edit’. Each controller either had 1 or many to be connected.

Now lets say we run into failure on the page with the user typing in the wrong information. We would create a file for the program to run to grab and give a failure response with a return link to the login page.

Step 4-Finish Line

After adding my folders and files to my views. It was time to add my HTML code/links, headers, and buttons. So when someone enters a page they have options, whether they want to view a book, edit a book they see, create/ add a book, delete a book or just simple log out. Once I got everything up and running, plus making sure all links and buttons work, my project is now complete! Yay!

I will admit, parts of this project took me some time. Time to look back and truly understand what is happening with my code and why it wasn’t working. Learning to breathe and carefully look at my code and figure out my errors on google helped tremendously! I did start off with doing to much at the beginning and wanting to add more that was truly not needed of this project.

--

--