CSC/ECE 517 Fall 2013/ch1 1w26 as
Introduction to MVC
MVC refers to Model-View-Controller is an architecture pattern applied to separate the user’s view and his interaction with it.
It consists of 3 major components:
- The model: The model is the part of the architecture that is connected to the database and contains all the business logic behind the application
- The view: The view refers to how the user sees any information that he requested for
- The controller: The controller is the component that co-ordinates between the model and view, making sure that the user gets the data which he had requested for or his given instructions are being executed.
This kind of architecture is very popular among web applications.
MVC in Rails
To understand how the MVC architecture works in the background, let us take an example of web browsing. The following is the sequence in which the process goes about:
- The user enters the URL of the page he wishes to visit.
- The controller interacts with the model to fetch the particular request and invokes the view to render the new view.
- The view renders the required web page and displays it to the user/client.
In Rails, the model, view and controller and separate entities functionally which are knitted together to perform the required functions.
Example from a Rails application:
- Incoming requests are sent to a ‘Router’ which determines where to parse the request and forward it.
- The request if forwarded to a controller, wherein a particular method/action is called.
- Using the data available in the request, the action interacts with the model or it causes other action to be invoked.
- The action then prepares the information for the ‘View’ which renders the output to the user.
Rails handles all the low-level details which would otherwise take so long to do it by self, so that you can concentrate on the application’s core functionality.
Rails principles
- Convention over configuration: By using standard conventions, we can avoid the need for explicit mapping such as deployment descriptors. The naming conventions would automatically perform mapping (like between controller and view)
- Don’t Repeat Yourself (DRY): While using Rails, we make sure that the required information we might user time and again to execute the same functionality is stored in a single unambiguous place.
Inside Rails
- Active Record (Model) -
- Action Pack (View and Controller) – This bundles both the view and controller components which are quite intimate. The controller supplies data to the view and also receives events from the pages generated by the views.
VIEW:
Action Pack provides View support by helping create either all or part of a page to be displayed in the browser. The dynamic content is generated by templates.
CONTROLLER:
Coordinates the action between the user, the views and the model. Rails handles most of this functionality behind the scene and lets you concentrate on the application-level functionality. The controller also routes external requests to internal actions, manages caching, manages helper modules and sessions.
References
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller