Enterprise applications are considered to be complex to design and develop. Technologies such as J2EE and .Net augment this generalization. But there are technologies and frameworks that beg to differ. Ruby is one of those technologies/languages and Ruby-on-Rails (also called RoR) is such a framework.
Getting Started with Ruby on Rails - Understanding the Terminology of Ruby-on-Rails (Page 2 of 4 )
RoR is based on Ruby and it follows the MVC pattern. Now what is this MVC pattern and what are its types? MVC is short for Model View Controller where the components (obviously) are Model, View, and Controller. MVC comes in two flavors, MVC-Model I and MVC-Model II. Before going into the types of MVC and the type followed by RoR, let's look at each of the components of MVC individually.
The definition states that Model is “The domain specific representation of information on which the application operates.” Essentially, the Model is responsible for maintaining the state of the application. And state refers to the data contained within the instance of an application at a given instance.
In short, Model deals with the data of the application. However, in the absence of the context of usage, data is raw and useless. So Model contains the logic that transforms the data by enforcing the business rules that apply to the data. For example, if the application deals with customer orders, the Model would contain (obviously) the data related to the orders. Along with that, the Model would also contain the logics that govern the discounts and so forth.
The main function of the View component is to render the Model into a suitable form for user interaction, generally a GUI element such as textbox, combo box, and so on. To elaborate, the data within the Model has to be represented in a form that humans can read. This representation of data into a human readable form is done by View.
View uses various GUI controls to present the data to the user. Many times, the Model contains sets of data, out of which only the relevant ones have to be shown to the user. To achieve this, the View includes logic to retrieve the required data from a collection of data present in the Model. The logic present in the View is also known as Presentation Logic. If I use the previous example, if the Model contains data about different orders from the same customer and if all of the data from the orders must be displayed a single order at a time, then that logic would be in the View.
According to its definition, the Controller “Responds to an event, typically a user action and invokes changes on the model and perhaps the view." That means controllers receive an event from the user (input), interact with the Model by either retrieving new data based on user input or updating the Model based on the data from the input, and display the appropriate view to the user. In a nutshell, the Controller defines the flow of application logic and thus orchestrates the application. Relying on the earlier example, when a customer updates an order, it is the controller that, in reality, updates the Model and shows the view according to the updated data.
Pictorially, the interaction between the Model, View and Controller would be:
On the basis of how the Controller is implemented, there are two types or models (not to be confused with the Model of MVC; here model means type) of MVC architecture.
The MVC-Model I is also known as the Page Centric MVC. In this type of MVC model, the Controller is embedded along with the View (or Page). The Model is separate. That means if a templating technology (like RHTML, JSP) is used, the logic of the Controller would be embedded with the template created using the templating technology.
In MVC-Model II, the Controller is separated from the View. If the components of MVC are taken as layers, then, in model II, all the layers are separate. The logic for the Controller is no longer embedded within a template (such as RHTML. JSP).
RoR follows MVC-Model II, which implies that the View and Controller are placed separately. View is placed in RHTML and Controller logic is embedded in ruby files. Hence Ruby-on-Rails based applications have to follow the MVC-Model II pattern. The steps required to create such an application are the focus of the next section.