Wednesday, November 11, 2009

Rails MVC

Ruby on Rails is an MVC framework for web application development.

The Model View Controller principle divides the work of an application into three separate but closely cooperative subsystems.

The Model is responsible for maintaining the state of the application. When the state is transient, data will last a few transactions with a user. When the state is permanent, data will be stored using a database. A Model will maintain the data. The Model enforces all the business rules and logic that apply to the data. The Model acts as both a gatekeeper and a data store.

ActiveRecord plays the role of Model in Rails. It is the module for handling business logic and database communication. It maintains the relationship between object and database and handles validation, association, transactions, and more.

The View is responsible for generating a user interface, normally based on data in the Model. The View handles graphical user interface objects and presentation logic.

ActionView plays the role of View in Rails. It is the component that handles the presentation of pages to the client. A presentation of data in a particular format, triggered by a controller's decision to present the data. They are script based templating systems and very easy to integrate with AJAX technology.

The Controller orchestrates the application. The Controller receives events from the outside world (normally user input), interact with the Model, and display an appropriate View to the user. The Controller handles user interface and application logic.

ActionController plays the role of Controller in Rails. It is the component that handles browser requests and facilitates communication between the Model and the View. 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, massaging it) into a form that fits the needs of a given view.

The MVC process:
  1. The browser, on the client, sends a request for a page to the controller on the server.
  2. The controller retrieves the data it needs from the model in order to respond to the request.
  3. The controller hands the retrieved data to the view.
  4. The view is rendered and sent back to the client for the browser to display.
Following a set of conventions and partitioning your functionality appropriately, code becomes easier to work with and your application becomes easier to extend and maintain.

Separating a software application into MVC:
  • improves scalability
  • make maintenance easier
  • promotes reuse
That's the simplistic overview of Rails and the MVC architecture pattern. Thats what MVC means, and thats how rails accomplishes it. Rails starts with the skeleton of an application already prepared. The configured skeleton application along with the MVC design creates a robust language.