What is MVC? First things first, it stands for Model View Controller. It refers to an architecture for creating web applications. In simpler terms, it’s used to help build frameworks for applications. Please keep in note, MVC is not specific to Ruby on Rails and I’ll be speaking about it in the context of Rails. Each one has its own separate responsibilities. One way that helped me understand the MVC framework is the restaurant analogy. Keep the image below in mind while I explain each one in detail!


Think of the model(s) as the chef(s) in the restaurants. They represent the data and logic of the app. They’re responsible for manipulating and saving data and as a result, have a relationship with the database. In addition, they contain validations, associations with other models, and methods that interact with the database. Look at the code below for example!


Think of views as the customer(s) in the restaurant analogy. It is responsible for rendering what the user sees and is what the user interacts with. In Ruby, the code is written in ERB or Embedded RuBy. Look at the code below for example!

This would render something along the lines of this.


The controller is like the waiter in a restaurant. It connects the model and the routes. It only has access to the instance variables provided by the controller. If you remember the first image, the user essentially interacts with the view and then it goes along to the controller, eventually going to the model and then goes full circle. Just like a waiter, it takes requests from the customer (or user) in this context, goes to the chef (or model), gets what the user was requesting (or food) and brings it to the user. Look at the code below for example!


In summation, think of the MVC as an architectural design for web applications in Rails. Each one has a separate responsibility, each one working in conjunction with each other to meet the users’ needs. Please note the code snippets I used are just examples and your code does not have to mirror it! Check out the resource below for more information!

Flatiron School Alumni & Software Engineer https://jonathanwong110.github.io/JonathanWongPortfolio/