CSC/ECE 517 Fall 2013/ch1 1w27 ma
MVC architecture structure in Ruby on Rails
What is MVC
MVC is a design pattern and was developed in 1979 by Trygve Reenskaug. MVC dictates that the system be split into three distinct parts, a Model, View and Controller. This approach organizes the code into separate components, thereby achieving separation of concerns and facilitating maintainability.
Model
- The Model generally contains the data for the application and is usually linked to a database back-end. This is the data structure that the application uses.
- It contains the application state and also most of the business logic. The model has no knowledge of the user interfaces.
A central component of Rails is the class ActiveRecord, which maps relational tables to Ruby objects and thereby to the data manipulated by controllers and shown in views
View
- The view refers to the interface that is presented to the end-user. The view does not do any processing, but simply acts as the presentation layer, displaying the application data.
- Rails contains a very nice template language for .erb files that combines pure HTML with embedded Ruby code.
Controller
- The controller receives events from the outside world [ or through some view] and performs some processing
It interacts with the model and redirects to the appropriate view.
MVC Architecture Working
MVC Architecture working can be explained as follows:
- 1. The browser makes a request, such as http://mysite.com/video/show/10
- 2. The web server receives the request. It uses routes to find out which controller to use:the default route pattern is “/controller/action/id” as defined in config/routes.rb. In our case, it’s the “video” controller, method “show”, id “10″. Controllers do the work of parsing user requests, data submissions, cookies, sessions. In this case, the show method in the video controller knows it needs to lookup a video. It asks the model to get video 10, and will eventually display it to the user.
- 3. Model(ActiveRecord) maintains the relationship between Object and Database and handles validation, association, transactions, and more. They talk to the database, store and validate data, perform the business logic. This subsystem is implemented in ActiveRecord library which provides an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records.
- 4. In this case, the model retrieves video 10 from the database.
- 5. Model sends the retrieved data ie video 10 in this case to the controller.
- 6. Views(ActionView) are what the user sees: HTML, CSS, XML, Javascript, JSON. Views are merely puppets reading what the controller gives them. This subsystem is implemented in ActionView library which is an Embedded Ruby (ERb) based system for defining presentation templates for data presentation. In our example, the controller gives video 10 to the “show” view.
- 7. The show view generates the HTML: divs, tables, text, descriptions, footers, etc and returns it to the controller.
- 8. The controller returns the response body(HTML, XML, etc.) & metadata (caching headers, redirects) to the server. Controller is 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. This subsystem is implemented in ActionController which is a data broker sitting between ActiveRecord (the database interface) and ActionView (the presentation engine). The server combines the raw data into a proper HTTP response and sends it to the user's browser.
MVC Rails
Directory Structure
app
This folder organizes the different application components. There are the following sub-directories
- Model [/models]
- View [/views]
- Controller [/controllers]
- Mailer [/mailers]
- Helper [/helpers]
- Asset [/assets]
There are sub-directories that hold the view (views and helpers), controller (controllers), and the backend business logic (models).Core application (app) code, including models, views, controllers, and helpers
File/Directory | Purpose |
---|---|
app/ |
|
app/assets |
Applications assets such as cascading style sheets (CSS), JavaScript files, and images |
config/ |
Application configuration |
db/ |
Database files |
doc/ |
Documentation for the application |
lib/ |
Library modules |
lib/assets |
Library assets such as cascading style sheets (CSS), JavaScript files, and images |
log/ |
Application log files |
public/ |
Data accessible to the public (e.g., web browsers), such as error pages |
script/rails |
A script for generating code, opening console sessions, or starting a local server |
test/ |
Application tests |
tmp/ |
Temporary files |
vendor/ |
Third-party code such as plugins and gems |
vendor/assets |
Third-party assets such as cascading style sheets (CSS), JavaScript files, and images |
README.rdoc |
A brief description of the application |
Rakefile |
Utility tasks available via the rake command |
Gemfile |
Gem requirements for this app |
Gemfile.lock |
A list of gems used to ensure that all copies of the app use the same gem versions |
config.ru |
A configuration file for Rack middleware |
.gitignore |
Patterns for files that should be ignored by Git |
Naming conventions
References
Ruby on Rails for web application Rails Directory structure Ruby on Rails MVC Framework