MVC - Model View Controller

slj2222

slj2222

Posted on April 20, 2022

MVC - Model View Controller

It is very important to know and understand what MVC, or the Model View Controller is when you are starting to learn Rails. MVC - Model View Controller is the architectural structure of Rails (and many other programming languages) projects that provides organization pattern, as well as an efficient means of separation of responsibilities. It is broken down into three parts, the model, the views, and the controllers. This blog will highlight what each part's role is and how they all work together.


Model
First, the model. Models are used to perform any business logic that is relevant to that specific model through ActiveRecord. Models communicate with the database to perform algorithms on the data (i.e. CRUD actions, create, read, update, delete). Most of the logic applied in the application will live in the model, including model relationships and validations. Models are classes that inherit from ApplicationRecord, which is a class that inherits from ActiveRecord::Base, which is what allows us to communicate to our database through the models.

Controller
Next, the controller. Controllers are the "middle man" in our application, which means they communicate between the data and the presentational parts of the application. They are responsible for determining how specific requests are handled. They will communicate with the model to retrieve data, then communicate that data with the view. Really, the controller is the engine behind the application that drives the experience between the web browser and our server. Each controller inherits from the ApplicationController, which is a class that inherits from ActionController::Base, which allows us to define actions that are associated with different routes in our application. For example, when our user makes a request at a particular route, our controller determines how to handle that request. Controllers will typically have a few different actions that are associated with an individual route. Each one of the actions will perform different behaviors by storing data inside an instance variable to be passed to the view.

View
Finally, the View. The views are strictly the presentational parts of the application and they only contain code that is specific to what the user should see. The views do not contain complex code, as there is no direct communication to the database or the models. Again, the views only have one responsibility, which provides a strong and dynamic webpage for the user.


Perhaps the most important part of the MVC is the naming convention. Here is a quick highlight of the naming conventions for each part of the MVC:

Model - should be singular, followed by ".rb"
(i.e. company.rb)

Controller - should be a plural representation of the model, followed by ".rb"
(i.e. company_controller.rb)

View - should match the action named in the controller to provide an implicit render.
(i.e. in the controller, there is a method called "show". The corresponding view should be called show.html.erb)


Hopefully this intro helps better your understanding on the MVC, each part's role and how they all work together.

💖 💪 🙅 🚩
slj2222
slj2222

Posted on April 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

MVC - Model View Controller
rails MVC - Model View Controller

April 20, 2022