9 Apr 2009

Q1. How is Rails structured to follow the MVC pattern?

Challenge Problems:
Q1. How is Rails structured to follow the MVC pattern?

Consider our project and examine the directories where Rails is located. If the data model is called Taxi (it is convention to name the model beginning with an upper case letter).
The Model is a Ruby class located in app/models/taxi.rb
The SQL table is taxis – the pluralisation of the model. In our project we have 2 tables as passenger_origin and passenger_destination, where the table row = an object instance and each of the columns = an object attribute.
The Controller methods live in app/controllers/taxi_controller.rb
Each controller can access templates to display the input screen and methods for action.
The Views are kept is app/views/taxi/*.rhtml, where each *.rhtml maps to a controller method.
In Rails, the view is rendered using RHTML or RXML. According to the wiki page at http://wiki.rubyonrails.org/rails/pages/UnderstandingViews, RHTML is HTML with embedded Ruby code and RXML is Ruby-generated XML code.

Answer:

1. After decompressing Instant Rails 2.0 in hard drive C:\, we can go to the command line and run "rails taxi". At that moment, the taxi project is built and the related directories are created under c:\InstantRails-2.0-win\rails_apps\taxi\. The Diagram-33 is shown below to describe the directories structure of the taxi project.

Diagram-33 - The directories structure of the taxi project

Actually, all projects are placed under c:\InstantRails-2.0-win\rails_apps\. In c:\InstantRails-2.0-win\rails_apps\taxi directory, there is app subdirectory containing controllers, models, views and helper sub-subdirectories. The directories exactly match the MVC pattern. The Controller sub-subdirectory has the files that define controllers method. The Models sub-subdirectory has the files that are the ruby classes. The Views sub-subdirectory has the rhtml files to match the controller methods and display to users.

2. Next, we changes directory to taxi and then we can run "ruby script/server" to start the taxi porject into web. The taxi system would be run at the tcp port 3000
by default. The default tcp port can be changed. Here I change it to 4000. The diagram-34 shows the taxi project start at 4000 port.


Diagram-34 - Taxi Project starts at tcp 4000 port

3. After that, we can create the database in mysql. In our taxi system, we can create 3 environments. They are developement, test and production. In mysql command prompt, run "create database taxi_development;", "create database taxi_test;" and "create database taxi_production;". We will use taxi_development database to develop our taxi project.

4. In order to make ruby applications successfully connect to mysql, we need to modify database.yml under
c:\InstantRails-2.0-win\rails_apps\taxi\config. The following is the content of database.yml...
development:
adapter: mysql

database: taxi_development

username: root

password:
host: localhost

test:
adapter: mysql
database: taxi_test
username: root

password:
host: localhost
production:
adapter: mysql
database: taxi_production

username: root
password:

host: localhost

5. Next, we can run "ruby script/generate model Taxi". This command generates the taxi ruby class. There is a taxi.rb file containing this class. This class is the Model of MVC approach and so-called ActiveRecord. The diagram-35 show the app/models directory contains taxi.rb.

Diagram-35 - app/models directory and taxi.rb file contains the taxi class

6. Besides, we can also run "ruby script/generate controller Taxi". This command generates controller methods. There is a taxi_controller.rb file containing the controller methods. The controller methods are also called ActiveController. The Diagram-36 shows the app/controllers directory and the taxi_controller.rb file.


Diagram-36 - the app/controllers directory and the taxi_controller.rb

7. Finally, we can create rhtml file in app/views directory.

Ruby on Rails follows MVC pattern.










No comments:

Post a Comment