[RAILS] I seriously summarized MVC

Introduction

I'm using Rails as a matter of course, When asked "By the way, what is MVC?", I felt that it was unexpectedly difficult to explain ... I tried to summarize it once again in order to make it my own language.

What is MVC

Abbreviation for Model View Controller This is a web application design pattern that divides the entire program into three elements: data display, data processing, and database management.

In the case of Rails, MVC processing is roughly performed according to the flow shown in the figure below (self-made…: sweat_smile :).

スクリーンショット 2020-12-18 15.38.32.png

Each function of MVC will be explained in order according to the processing flow.

Before entering MVC ... What is routing?

First receive the request from the client. ** It has a role like "correspondence table" ** that specifies the destination from the request contents to the controller that has the corresponding processing.

C: Controller

This is the part that controls the processing of ** requests from clients **. Executes the action corresponding to the request and returns the aligned contents to the client as a response.

To gather the information needed for the response

--Tell the model and prepare the necessary data --Pass the data required for screen display to the view

It has a role like "bridging" with other functions.

M: Model

It has a function like ** "window" that interacts with the database **. Access the database based on the instructions from the controller, This is the part that acquires the necessary data and processes (inserts / updates / deletes) the data.

It also plays a role in making detailed settings for the data stored in the database, such as ** data validation (validation) and association between tables **.

V: View

This is the part that sets the final ** appearance of the web page **. The data passed from the controller is formatted with HTML etc. and displayed on the screen as a response.

You may also ** get the data from the input form and pass it to the controller **.

Benefits of MVC

The following two points are major merits.

--Separated by function, easy division of labor ――It is less affected by changes in other functional parts, and maintainability is improved.

At the end/impression

I thought I knew MVC through personal development, I feel that I was able to deepen my understanding again by putting it together in an article.

This is a poor article for beginners, but I hope it will be of some help to you. Thank you for reading to the end.

Development environment

Ruby 2.6.5 Rails 6.0.3.4 MySQL Visual Studio Code (GoogleChrome)

Reference articles / books

[Book] Ruby on Rails6 Super Introduction/Yano Palm Tatsu [Official] Rails Document [Qiita] A summary for a quick understanding of the concept of MVC. [Qiita] About MVC model [Qiita] Let's summarize Rails MVC [Pikawaka] Carefully explain the MVC framework from 1!

Recommended Posts

I seriously summarized MVC
I summarized the collection framework.
[day: 5] I summarized the basics of Java
[Java] I personally summarized the basic grammar.