This post will help new Rails developers to understand Modern Application development flow by teaching them MVC.
MVC is an acronym for Model View Controller. It's a design pattern for web application development. Ruby on Rails uses this pattern by convention. It separates the core components so that they work independently and change in one does not affect the other.
See this picture to understand how the information flows inside a web developed using MVC pattern:
HTTP is a protocol that is widely used in client-server environments. It's based on request-response. The client or browser is the one who makes a request to the server and gets a response.
It means that the browser takes the first step in communication between client and server. This is the time now that we start our ride of MVC on Rails.
Rails app provides routes, a type of path for HTTP requests to land in application safely. See this for detailed info about Rails Routes.
Browser request hits controller directly, and it's controller role to pass this request to Model for database or to view to get a view template. Controller when gets a response gives the browser back that response which shows that it received information for the user.
Let's go through each component of MVC one by one!
Model is the M in MVC. Model is used to interact with the database in your application. In Rails, it uses Active Record to access the database. The model can create validations and associations between models. See Rails Guide on Model.
The view is used to present data fetched by the controller from the Model. There are formats to present that data: PDF, HTML, JSON, etc. But most of the time web pages use HTML and CSS for styling. See Rails Guide on View.
Controller by name shows what it's responsible for. It's the main pillar of MVC architecture. It gets a request from the browser, passes it to the Model if it needs any data. After getting a response with data from the Model it passes it to View, which makes data presentable. It finally gives a response to the browser with success if the request is fulfilled or with an error. See Rails Guide on Controller
Advantages of MVC architecture:
Disadvantages of MVC architecture:
There are not many disadvantages of MVC, so it's useful to look at advantages only!
That's it! Thanks for reading and supporting me!