Ruby on Rails: HTTP, MVC and Routes

a little bit about the web and Ruby on Rails

Here we’ll understand the basics about how the web, the routes and the MVC architeture work, using Ruby on Rails web framework. Let’s go!

1. How the web works?

Basically, it works through HTTP (Hypertext Transfer Protocol).

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. — Wikipedia

The HTTP works like a request - response protocol in the client - server model.

We have a web browser (Google Chrome, for example). So we write “” URL, the client submits the HTTP request (request message) to the server. The server returns the HTTP response (response message — in that case, the response is the HTML from google website).

The client do the request and receive the response from the server.

2. The MVC architeture and Rails Routes

Now we understand how the web works, we’ll study about MVC architeture and Rails Routes.

MVC = Model — View — Controller

In that architeture we have the “separation of the concerns” among Models, Views and Controllers.

Model: “Maintains the relationship between Object and Database and handles validation, association, transactions” — it means that the model will maintain an extreme relation with the Database. Each model (can) represents a database table (in case of SQL databases). Besides that relation with the database, the model can create validations, transactions, associations between models, etc.

View: “A presentation of data in a particular format, triggered by a controller’s decision to present the data.” — it is the presentation of the request’s response. This presentation can be a big amount of format types: PDF, HTML, JSON, etc.

Controller: “The facility within the application that directs traffic, on the one hand querying the models for specific data, and on the other hand organizing that data (searching, sorting) into a form that fits the needs of a given view.” — The controller is the “Maestro”. It take care of the flow: uses models to do queries, parses data, make decisions among which format you’ll present the data, etc.

Let’s see how this architecture works graphically:

3. Let’s see an example:

An user digits an URL (http://localhost:3000/) in the browser, so it make a request for the server. The Rails Router verifies if there is an entry matching the URL requested.

In our case there is a route entry. In that case the flow goes to HomeController with a specified action (index action). In the controller we can use the model to make queries in the database (CRUD — Create, Read, Update, Delete), and render the view as the server response.

* (The index action will render the index view — index.html)

4. Bye!

That’s it guys! I want to update (include more details) that blog post. The idea is to share a great content and the community helps improving this post! ☺

I hope you guys can appreciate the content and learn more about how the MVC architecture works in Rails.

If you enjoyed this piece, you might also like

SQL Database and Rails