How Does MVC Work in Rails?

Models, views, and controllers provide the architecture for Rails. Known as MVC, this format means that the models are objects, the view is the presentation layer, and the controller handles the processes and responses for the various events that occur.

Sign Up to Start Your Own Code Camp

Models, views, and controllers provide the architecture for Rails. Known as MVC, this format means that the models are objects, the view is the presentation layer, and the controller handles the processes and responses for the various events that occur.

That’s just a basic explanation, though. You need to look under the hood so you can understand the different processes. Understanding the different processes will help you when you develop Rails applications.

First, though, you need to understand how basic web architecture works. Then, you will be able to appreciate the MVC design.

Basic Web Architecture

If you set up a website using basic architecture, you will have two primary systems—the browser and the web page. The two systems have to communicate with one another to complete various tasks online.

In this system, the web page is the main communication hub. It is filled to the brim with code so it can fulfill its duties. Using that code, the web page makes all the decisions and sends information back to the browser.

In some cases, it also sends information to a database. If it has to communicate with the database, however, it requires even more code.

The MVC Process

While the basic web architecture focuses on two components, the MVC process breaks the system up into different parts. With this system, the browser communicates with the controller. The control contains code that allows it to make decisions based on the information the browser sends it. The controller then sends information to the model, which in turn sends information back to the controller.

Once the controller has the information it needs, it pushes it out to the presentation layer. The presentation layer, also known as the view, determines the HTML code and other factors. Then it sends that information back to the browser.

medium

Advantages

While you might like the simplicity of the basic web architecture, the MVC architecture has its advantages, the biggest of which is the ability to reuse code. With an MVC system, your code isn’t dependent on your data, so you can reuse it for other applications with ease, which saves you a lot of time. If you don’t like the idea of writing new code for every application, you will benefit from this system.

Also, since this system separates the concerns, you can benefit from true unit testing. You can test each layer independently of the others, which will provide you with better results than you get when the concerns are intermingled. Accurate testing is important to web developers of all skill levels, so this is a huge benefit. After all, the quicker you can isolate problems, the easier it is to fix the issue and move forward.

While MVC wasn’t just made for Ruby on Rails, it works especially well with this system. Its clean, intuitive nature allow for easy communications between various components, enhancing the experience for both the developer and the end user.

Jordan Hudgens

Jordan Hudgens

I've been a software engineer for the past decade and have traveled the world building applications and training individuals on a wide variety of topics.


View All Posts