ModelViewController is an application architecture that breaks down a program into three architectural components/segments.
Main Components
- Model: Manages data, logic, and rules of the application. It interacts with the database and handles data validation, associations, and transactions
- View: Represents the user interface, typically as HTML templates with embedded code.
- Controller: Acts as an intermediary between the Model and View. It handles external requests, queries models, and determines which view to render
Router: Maps incoming URL requests to the appropriate controller actions
Req-Res Cycle
- User makes request by accessing URL.
- Rails router identifies corresponding controller and action required.
- Controller interacts with the model to retrieve and/or manipulate data.
- The controller passes data into the appropriate View.
- The view generates the HTML response.
- Then the server sends it to user’s browser.
Excerpt from Odin Project:
The path through MVC
Once a request from a browser comes into your application, at the most basic level:
- The router figures out which controller to send it to (e.g. for your blog, the Posts controller).
- That controller asks the model (e.g. Post model) for data and any other tough questions it has.
- Then that controller passes off whatever data it needs to the views (e.g.
index.html.erb
), which are basically just HTML templates that are waiting for those variables. - Once the proper view has been pumped full of the data it needs (like the current user’s name), it gets sent back to the client that made the original request. Presto!
betterexplained.com has a deeper explanation of MVC.