Sinatra/ActiveRecord Project!

Photo by Luca Bravo on Unsplash

I am back with another project and my progress through the Flatiron Full-Time Online Software Engineering Bootcamp!

It has been an amazing journey so far, and I actually can not believe how much I have learned and how quickly I have been able to produce a dynamic web app. For Phase 2 of the boot camp we jumped from Object Oriented Ruby, to ORM (Object Relational Mapping) with SQL and ultimately learned how to build a basic dynamic web apps using the MVC(Models, Views, Controllers) principles. I will share my experience with building out my web app and some of the problems I faced while working on the project.

I created a web app that used dynamic webpages to essentially create and manage a content database on the back end through user interaction with the website and a couple of important ruby libraries that we learned how to use in phase 2. After learning about the SQL language and creating data tables, we then learned about how the internet and webpages works behind the scenes(on the back end). Learning about the HTTP requests was crucial to understand what way and how to build out the web app with the help of these ruby gems.

As a beginner to web developing, we were tasked with learning about Rack, Active Record and Sinatra, which are gems/libraries of the ruby language that were built to help developers with building out web apps quickly. Otherwise, without these gems, developers would be forced to build everything out from scratch which would take much much longer and I imagine they would lose sight of their original project plans. Thankfully, these gems metaprogram those methods and functionality we need for us to focus on the more important parts of the project. Understanding what they do from a high level overview is what is essential to understanding the concepts of developing web applications(to my understanding).

My web app is a basic content management web app about cats and their owners, named “Crazy Cat Lovers”. In this app, I successfully implemented MVC principles with web pages(views) where users have CRUD (Create, Read, Update, and Delete) functionality where they can create, edit, and delete cats on the website. I established a relationship between users and cats to set up the database on the back end where cats belong to users and users have many cats. The user and cat models interact through the controllers and views with HTTP requests to dictate whether a user creates, edits, or deletes, cat objects in the web app.

Some problems I ran into was at the very beginning; deciding what the project was going to be about, and also planning the file structure. Thankfully, our instructor pointed us to a very beginner friendly gem that was actually made by a Flatiron alumni. The gem is called Corneal. Corneal is a metaprogramming gem that essentially generates the Model-View-Controller file structure template and Sinatra functionality built in to the app. Without this gem, I would have had to set up the file structure manually, and create a connection to sqlite3 adapter and create a environment and database for my application to interact with on the back end via the ActiveRecord.

(Here’s a link to the readme of the Corneal gem: https://github.com/thebrianemory/corneal )

After setting up the project, the work began of setting up models, views, and controllers separately and bringing them to interact together. Setting up basics was a breeze thanks to corneal, but I did run into a few problems later on down the road.

One of the projects requirements was the use of validations, which I hadn’t heard of and did not seem to recall reading about them in the course materials for the second phase of the boot camp. I took to the pages of google, which directed me to: https://edgeguides.rubyonrails.org/active_record_validations.html#validations-overview

To my surprise a lot of google searches led me back to this ‘railsguides’ website. It seems that Rails, which we will be learning about in phase 3 of the boot camp, interacts with Active Record similarly to how Sinatra does. I learned that there are some pretty cool methods and helper methods that both Sinatra and Ruby on Rails uses or inherits from Active Record when associated. Anyways, validations are used for a table column of data to validate. As silly as the previous sentence sounds here is an example from my project when I used it:

picture denoting my User model with use of validations of uniqueness.

This snippet above is my User model. It shows the power of inheriting ActiveRecord Base Module, which shows how little code is actually written due to the metaprogramming that is inherited from the Base module of ActiveRecord. All I added to the model class is the validations stating that a username and password must be present when signing up or logging in to the webpage. This, in turn, tells my web app that from the SQLite database, through ActiveRecord, every username must be unique, which is a commonplace usage in most websites(i.e. two users cannot have the same username) to differentiate users apart. Adding that and a condition to my controller action for creating a new user, will simply not allow a user to create a new account if the username has already been taken. Using this metaprogrammed method of validations built in to ActiveRecord, it was much easier to create a small condition within the route than to spend additional time thinking on creating an involved method from scratch. Another method listed in my User class is the “has_secure_password” method. This inherited method actually allows the use of sessions(built in to Sinatra) and the ‘bcrypt’ authenticator gem to create secure and encrypted passwords so that users passwords can be protected from hackers. It was relatively easy to implement as well. You can enable the sessions through the main application controller just by typing “enable :sessions” and “set :session_secret”, which is amazing. I couldnt even begin to know what happens behind the scenes but lines and lines of code must have been written to create such a beauty as sessions.

Anyways, these were just a few things I wanted to highlight about the project and to demonstrate a little bit of what I have learned in how dynamic web applications work and more importantly, learning how to build my very own. This project assignment was a lot of fun to bring to life. I am excited to see what differences Ruby on rails brings to my understanding and learning of coding and web developing.

For being two months into this boot camp with Flatiron School, I am amazed at how much I have learned already and excited at how much more I will be learning. Stay posted on my projects as I will be back here in a month or so for the Phase 3 project and my progress so far. Until then…

Links to my Crazy Cat Lovers project assignment:

GitHub Repo:
https://github.com/wizbeck21/sinatra_catlovers

Demo Video:
https://youtu.be/t0WGgbgzwRc

Recent Flatiron Boot Camp Grad. Pursuing my dream to become a software engineer, sharing what I can to help others that are new to coding.