CSC/ECE 517 Fall 2013/ch1 1w27 ma: Difference between revisions
Line 99: | Line 99: | ||
==== Other Directories ==== | ==== Other Directories ==== | ||
*'''public:''' | *'''public:''' This directory contains static files and compiled assets, such as JavaScript files (public/javascripts), graphics (public/images), stylesheets (public/stylesheets), and HTML files (public). The files in this directory are accessible by anyone visiting the site, irrespective of authentication. Error pages (404) are usually stored here | ||
*'''tmp:''' | |||
*'''vendor:''' | *'''tmp:''' Temporary files are stored here. | ||
*'''script:''' | |||
*'''lib:''' | *'''vendor:''' This folder contains plugins, gems & libraries provided by third-party vendors. Example: database utilities library | ||
*'''doc:''' | |||
*'''script:''' This directory holds scripts to launch and manage the various tools that you'll use with Rails. | |||
*'''lib:''' For library modules. | |||
*'''log:''' Error logs for the server (server.log) and each Rails environment (development.log, test.log, and production.log). | |||
*'''doc:''' Rails can generate the documentation for your code automatically, if you provide comments in a certain expected format in your code. | |||
<br/> | <br/> | ||
---- | ---- | ||
==== Gemfile ==== | ==== Gemfile ==== | ||
Gem requirements for this app | Gem requirements for this app |
Revision as of 23:34, 16 September 2013
MVC Architecture Structure In Ruby on Rails
The aim of this wiki is to explain the explain the different components of a Rails application. It is directed towards a novice user who can read and get an overview, not the details of any particular kind of component. In depth discussion links are made available in the further reading section.
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.
How Rails MVC Works
MVC Architecture working can be explained as follows:
- 1. The browser makes a request, such as http://myblog.com/videos/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.
Directory Structure & Components
app/
This folder organizes the different application components. These include view (views and helpers), controller (controllers), and the backend business logic (models). There are the following sub-directories
- app/controllers: This is the sub-directory where Rails looks for the controller classes.
- app/helpers: The helpers are used to assist the model, view, and controller classes. Helpers make it possible to keep rest of the MVC code uncluttered.
- app/models: The directory for the models
- app/view: The views subdirectory holds the display templates to fill in with data from our application, convert to HTML, and return to the user's browser.
- app/view/layouts: Layouts directory contains the template files for layouts to be used with views. Examples of layouts are the common header/footer for the views.
- app/mailers: This directory contains all the mailer files used to send emails
- app/helpers: These are the helper classes used to assist the MVC classes. We need to keep as much Ruby code out of the views as possible, so helpers are mainly for use in that context. Helpers are the only methods one can access in the views, other than instance methods for an instance you have access to.
- Asset [/assets]
[AMRUT TODO]
config/
This directory contains the configuration code that the application needs. This includes database configuration, environment structure , routing of incoming web requests.
- config/environments: It is possible to execute some environment specific code in Rails. Rails automatically detects your current environment [development, test or production] and executes the environment file in this directory. For example, it will run config/environments/development.rb for the development environment. Thus we can tailor the behavior of the three Rails environments for test, development, and deployment with files in the environments directory.
- config/initializers: Any Ruby file stored in this directory is considered to be an initializer. It holds configuration settings that should be made after all of the frameworks and gems are loaded. Initializers can be further organised into sub-directories. If there are any ordering dependencies for the initializers,we can control the load order by naming. The initializers are run in alphabetical order.
- routes.rb:
The routing system in Rails is the system that examines the URL of an incoming request and determines what action should be taken by the application. Thus it recognizes URLs and dispatches them to a controller's action. The routes.rb file contains the rules Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.
db/
Usually, your Rails application will have model objects that access relational database tables. You can manage the relational database with scripts you create and place in this directory.
- db/migrate:
AMRUT TODO
test/
AMRUT TODO
Other Directories
- public: This directory contains static files and compiled assets, such as JavaScript files (public/javascripts), graphics (public/images), stylesheets (public/stylesheets), and HTML files (public). The files in this directory are accessible by anyone visiting the site, irrespective of authentication. Error pages (404) are usually stored here
- tmp: Temporary files are stored here.
- vendor: This folder contains plugins, gems & libraries provided by third-party vendors. Example: database utilities library
- script: This directory holds scripts to launch and manage the various tools that you'll use with Rails.
- lib: For library modules.
- log: Error logs for the server (server.log) and each Rails environment (development.log, test.log, and production.log).
- doc: Rails can generate the documentation for your code automatically, if you provide comments in a certain expected format in your code.
Gemfile
Gem requirements for this app
Rakefile
This file is similar to Unix Makefile which helps with building, packaging and testing the Rails code. This will be used by rake utility supplied along with Ruby installation.
README
This file contains a basic detail about Rail Application and description of the directory structure explained above
References
Ruby on Rails for web application
Rails Directory structure
Ruby on Rails Tutorial
What-are-the-benefits-of-MVC/
Further Reading
Rails Initialization Process
Routing in Rails
Routing Explained