A big part of learning web development is understanding programming languages and how to use them. In my coding bootcamp, Flatiron, one of the projects to complete is to build an application based on Sinatra.
Sinatra is a Domain Specific Language implemented in Ruby that’s used for writing web applications, created by Blake Mizerany. Most importantly, in my opinion, it is the best way to establish a foundation of understanding MVC (Model, View, Controller) before tackling Ruby on Rails.
My project is very simple. I built a web application that manages case loads on a very basic level. I used Sinatra to do it. I implemented SQLite3, ActiveRecord, RESTful routes, CRUD actions, and HTML. In this blog, I will go through and breakdown the thought processes and code formation used.
ActiveRecord is the database toolkit for Ruby. ActiveRecord is the M for Model in MVC. Basically, when the model inherits from ActiveRecord::Base, the models inherit a whole bunch of functionality. A whole bunch of methods you don’t have to write because it would… take…. foreveerrrrrrrrrrrrrrrr!
class class_name < ActiveRecord::Base end #inheriting methods from ActiveRecord::Base
This means that using this inheritance allows us to bypass writing setter/getter methods, validations of models, associations between models, allows us to communicate with the database, and much, much more.
RESTful Routes & CRUD. Now, I’m sure you’re asking yourself… what? Haha.. I asked myself the same thing. RESTful is a convention on how we handle HTTP verbs and URLs in the C for controller in MVC. Here is a controller using RESTful conventions and CRUD(create, read, update, delete) actions:
class Controller # GET: /cases # GET: /cases/new #Create # POST: /cases # GET: /cases/5 #Read # GET: /cases/5/edit #Update # PATCH: /cases/5 # DELETE: /cases/5/delete #Delete end
HTML/VIEWS. How do we see all the magic happen??
The V for View in MVC is how we see the code put together in HTML files in addition to using ERB tags. These files which turn into website pages are what the clients can see to get or view information.
Now, of course, there is a lot more of intricate details that go into building glorious applications, but this is the base.
Here is my github repo for the project: https://github.com/BrittanyTinnin/sinatra-case-tracker