Justin Clifford
3 min readOct 29, 2020

--

Model View Controller off the Rails

My name is Justin, and I’m a student software engineer and like most beginners everything is a struggle until you get it. Recently we started learning about the rails frame work, and I won’t pretend Iknow all about it, in fact the reason I chose to write this blog is to get more familiar with an architecture that Rails uses called Model View Controller.

In this blog I will try my best to explain what it is, what it does, and how it works, and hopefully by the end we’ve both learned something new. Let’s begin!

Now as scary as I may have made MVC sound it actually exists to make your life easier. The creators of Rails utilized this architecture to ensure applications had a specific organizational structure to them. Organization has a history of making things go at the least little bit smoother.

To go further i think of MVC like this: I shop online frequently and when I shop I’m going through a specific series of steps to get what i want. For example a store ships the product I need and I use their online store to request what I want to purchase and then eventually its in my hands. MVC isn’t doing anything too different from that. When you think about it I’m at home when i order (we’ll think of my computer screen as the view), i make a request to the website (this could be our controller) and the website informs the store (our Model)of my order to which that store can send a response back through the controller (the website ) and that controller can update my view with a confirmation my order was taken.

With that being said you’re probably wondering “well how does it do all that?”

Well your models are still just Ruby classes that are inheriting from active record so they handle the responses because they hold all the data as long as it has a table set up in the database. Since its a class you can create all kinds of methods and attributes you want so essentially the model is responsible for providing what your looking for. The controller is the middle man between view and the model, it’s job is to route data between the view and the model. In a rails app you have a routes.rb file that contains logic on where to find things a user may be looking for, the controller uses this file to find a path to what a user is looking for based on the path the user requested from the view. The way it does that is by the controller also being a class of its own that inherits rails’ action controller so it will hold class/instance methods and variables that will perform logic to the view and the model saving responses and requests to variables to be carried to and from.

The view holds the least amount of logic its really just meant to render what gets sent back from the database through an html markup. By using embedded ruby the view can rely on the controller to handle logical tasks for it. It’s actually pretty cool, once you have formatted the embedded ruby code with the appropriate html tags, it is so awesome to work through and see the pages change and update, and have functionality as you figure it out. I’m excited to dive deeper into rails and share even more knowledge, but i hope that this explanation has given you a good gist of what MVC does for your application, by sep erat ing functionality it keeps things more organized and streamlined this way your code can be much cleaner and less of a headache to work with. Thanks for sticking it out ’til the end and good luck on your coding journey.

--

--