MVC: Michael Vanishes Cupcakes?

Michael Corrieri
3 min readJun 20, 2021

--

Model-view-controller (MVC) is a software design pattern which divides program logic into three associated elements. MVC was first brought about in the late 1970s by computer scientist and professor Trygve Reenskaug. MVC was implemented and used for Smalltalk (an object oriented language used back in the day) but was only made notable in 1988 when The Journal of Object Technology (JOT) named MVC as a general concept. There are many variants of MVC including: hierarchical model-view-controller (HMCV), model-view-adapter(MVA), and model-view-presenter(MVP) but for this blog we will focus on MVC specifically for Rails applications.

So enough with the history lesson, let’s look at some MVC diagrams and show an example.

https://bogotobogo.com/RubyOnRails/RubyOnRails_Model_View_Controller_MVC.php

Above we have an example of how MVC connects and works with requests, routes and our database. Following the numbered steps above, our browser sends a request to a web server which is then passed on to the rails routing engine. The router gets the request and then redirects us to the specific controller class based on the URL. The controller will then talk to our model and then render what we want to view on the user’s side.

Model -

is in charge of handling validations, associations and transactions. It is also in charge of the relationship between our database and object. In association with ActiveRecord, we are able to save, alter and delete data from our database table. We can also create associations between models using our AR methods (has_many & belongs_to)

View- The response body content

Presents data in a specified format which is determined by the controller. We can simply think of this as what the user is shown after the MVC does its work.

Controller- The decider

Accepts input and sends specific commands to the model or view. The controller accepts input from our user and then is able to interact with our data model objects. For example in our user_controller we have a create action which allows us to create a new user for our app as long as the user input is valid.

But wait? How do we get our URL and controller to talk?

Routes!

Routes are just matchmakers which take a URL which is requested and hand it over to a specific controller. To implement RESTful routes we go into the config/routes.rb in our Rails app and define our HTTP verb, URL, and connect it to the controller action. Below is an example from an app which would be all about ham sammiches.

I enjoy creating ham sammiches, maybe you do too

You might have noticed that I did not include an image or diagram for views…well that is because in current projects (at least for our bootcamp) we are not using the views element. So is it really still MVC? Interesting…

--

--

Michael Corrieri

Software Engineering student at Flatiron School. Environmental & Occupational Health Sciences MPH. Based in Brooklyn, NY