Introduction to Ruby on Rails and how MVC works!!

Haarika Ramadugu
Nerd For Tech
Published in
6 min readNov 23, 2021
Image Source

As a developer, I often copy-paste some parts of the code from previous projects. I am a lazy person, no two ways about it, but the copy-paste is perfectly avoidable. For, it takes away my time from focusing on the functionality of the code. Instead, I have to spend on setting up the file structures and some project semantics.

Thank god for David Heinemeier Hansson, who created Ruby on Rails in 2004.

Yeah, I know we can modularize so we can re-use and all that. But still, there remains a part that still needs to be ctrl+c ctrl+v. Where you ask, configuration files. Devil is in the detail they said. There are so many details in those files it is the true manifestation of the devil. Do not even get me started on the issues originating from configuration files.

Anyway, the purpose of this article is to go over the MVC framework in detail, using ruby on rails, and how it makes our life easier.

Jargon Alert:

When I refer to the client, it is an app or a browser or wherever you are accessing that particular webpage.

Now, what exactly is Ruby on Rails

Ruby on Rails is a framework that handles the repetitive code common across apps and allows you to focus on the other functionality of the app. It provides tools to the developers to build a web application. Some of the components are almost common across all the ruby apps. Like routing, asset management, database connections, the list of reusable components goes on!!

This framework is a set of Ruby code libraries. Since the entire codebase is open source, you can review and contribute to the framework and understand how it works. But the main reason this is so popular is that it is an MVC framework.

What is MVC

MVC stands for Model-View-Controller. This architecture helps to structure the application by segregating Data, Business-logic, and UI. It has, you guessed it, three components:

Model: This component is where we do all the data management, parsing, fetching, basically everything data.

Controller: It handles business logic, data flow from model to view, and incoming user requests.

View: As the name suggests, this component is responsible for how and what the user views.

Ruby on Rails makes implementing this architecture easier. It speeds up the productivity of a developer by enforcing the structure of the application. i.e., Rails organizes the folder structure to file and variable naming, and the designer must follow. It is the iPhone of MVC frameworks. It lets you do things effortlessly, but only the way it likes.

It has a few benefits: Standardized structure and program conventions, the decision-making process is much easier and allows powerful abstractions in the code!!!

MVC Architecture

In the previous section, we came to know that Ruby on Rails follows MVC architecture. But what exactly is MVC architecture?MVC stands for Model-View-Controller.

One way to speed up the productivity of a developer is by enforcing the structure of the application. That means Rails organizes the folder structure to file and variable naming.
It has a few benefits: because of its standardized structure and program conventions, the decision-making process is a lot easier and allows powerful abstractions in the code!!!

MVC Architecture

In the previous section, we came to know that Ruby on Rails follows MVC architecture. But what exactly is MVC architecture?MVC stands for Model-View-Controller.

Rails’ follows the principle of convention over configuration. For desigining an MVC based application, one must follow these conventions. These conventions help free-up developers’ time from making decisions about folder structure, file and variable namings, routing, etc. As a designer, you have to follow the rules and can only do things a certain way.

But that buys you time that you can use to build better applications, add more features, or anything you fancy because you are no longer preparing for the design. You are designing. Rails are created with the convention over configuration and this holds for how the MVC structure is set up.

Grocery store example

In this example,

An app/models/grocery.rb is the model file that will contain: validations, database, and any custom logic.

An app/controllers/groceries_controller.rb will contain the CRUD methods to manage the data flow for the grocery behavior.

An app/views/groceries will contain a corresponding view for each page that the user will access. In our example it is index.html.

One important thing to remember here is each layer is independent of the other.

Model

The model file is a Ruby class. It has a corresponding database table and will inherit from the ActiveRecod::Base.That means that it can access several methods from Ruby libraries that will assist in working with the database. Such as creating methods and data attributes that the controller or view should not know for security reasons.

Controller

In the example, the resource is/grocery. Each route defined in the routes file must have a corresponding method/function in the controller.

When the request comes, Rails uses the route file to determine which controller method to run. Once it interprets the request, the designated route-controller method will run.

controller

Controller Action: `groceries#index` indicator tells the Rails routing system that control passes through the “GroceryController” index action.

Also, the majority of the business logic is part of the controller. That means any data validations, custom logic, and extending classes if out of scope is written in the controller file. When the controller method runs, it accesses the data attributes from the model and forwards it to the correct view.

View

View layer should contain the least amount of logic in MVC architecture. The role of the view is to render the data received from the controller. The controller can also pass the information on how to present the data. For instance, you will get the price per unit for a given grocery. But we can display the cost by user global metric preference as per gram or lb. The controller can also request some fields to be disabled or mandatory/optional. Also, all the creative work happens here, whether an input should be a dropdown or a radio button. Which format enhances user experience. These are some of the examples of what the view component does.

Now that we know about each layer in MVC architecture. Let us connect the dots!!!

Request/Response Flow in MVC

MVC
  • A client sends a request to the webserver.
  • The web server accepts the request from the controller component.
  • The request is then assessed and appropriately forwarded to its route (`/grocery`). The route interprets the request and maps with the controller method.
  • The controller then uses the model to access the data from the database. It then manipulates the data based on any business logic requirements.
  • After validation, it arranges the data for the view to comprehend.
  • And now it is sent to the view for rendering (index.html).
  • Once it receives the data, the view creates a corresponding response to be sent back to the webserver.
  • This HTTP response, along with the JSON data, goes back to the client.

Conclusion

MVC architecture is an important concept to understand since most web applications are built based on this architecture. In this architecture, each layer is independent of the other. Hence it is easy to modify one part without affecting the other. Overall, the development process is faster since each layer can work parallelly and independently. This architecture also helps developers to debug faster!
You can also refer to the Rails documentation for more detailed info.

Happy Coding!!!

--

--

Haarika Ramadugu
Nerd For Tech

Full- Stack Engineer Intern, always learning. Showing my perspective of my learnings through writing. https://www.linkedin.com/in/haarika-ramadugu/