4/07/2009

Workshop 2-part 2

Challenge Problems:

1. How is Rails structured to follow the MVC pattern?
I draw the diagram to explain how is Rails stuctured to follow the MVC patten in my taxi booking system, please refer the below diagram, by the way, you can enlarge the diagram by double-click it.
























Diagram 1. MVC on ROR

2. Apply the MVC design approach to our Project: Online Taxi Booking System.

HINT: Begin with a single model, single view and single controller classes. This will give you a head start to the next workshop: Online Taxi Booking System: SQL and Database design.

Step 1. Create web appliaction project named app by issuing the command "rails -d mysql C:\rails_apps\app".

Step 2. Create a controller named taxi in the app by issuing the command "ruby script\generate controller taxi".

Step 3. Create a model named taxi in the app by issuing the command "ruby script\generate model taxi".

Step 4. Create a rhtml file name index.rhtml in C:\rails_apps\app\app\taxi\list.rhtml.

Step 5. Create three databases in mysql, they are app_production, app_development and app_test.

Step 6. Add the root account password of development, test and production database configuration field in the database.yml file which is located at C:\rails_apps\app\app\config\database.yml, please refer the below diagram

























Diagram 2. Database.yml configuration


Step 7. Create a migration file named taxis in the app by issuing the command "ruby script/generate migration taxis".

This will create the file db\migrate\001_table_name.rb. A migration file contains basic Ruby syntax that describes the data structure of a database table.

Step 8. Edit C:\rails_apps\app\db\migrate\001_create_taxis.rb and add with following code into CreateTaxis class

def self.up
create_table :taxis do t
t.column :suburb, :string
t.column :numpass, :string
t.column :taxitype, :string
t.column :time, :datetime
end
end

def self.down
drop_table :taxis
end

Step 9. Create the table in app_production database by issuing the command

"set RAILS_ENV=production" and "rake db:migrate".

Step 10. Modify the model file of taxi which is located at C:\rails_apps\app\app\models\taxi.rb with following code:

validates_presence_of :suburb,:message=>'Error Message, suburb cannot be null!'
validates_presence_of :numpass,:message=>'Error Message, numpass cannot be null!'
validates_presence_of :taxitype,:message=>'Error Message, taxitype cannot be null!'
validates_presence_of :time,:message=>'Error Message, time cannot be null!'

Step 11. Modify the controller file of taxi which is located at C:\rails_apps\app\app\controllers\taxi_controllers.rb with following code:

def list
@taxis = Taxi.find(:all)
@current_time = Time.now
end

Step 12. Modify the view file named list.rhtml which is located at C:\rails_apps\app\app\views\taxi\list.rhtml. Please refer to the below diagram.
























Diagram 3. List.rhtml

Step 13. Insert some dummy data into app_production.taxis like this :
mysql> insert into taxis (suburb,numpass,taxitype,time) values ("Hong Kong", "2", "standard", 20090130);

Step 14. Modify the route.rb which is located at C:\rails_apps\app\config\routes.rb with the following code:
map.connect 'see/', :controller => 'taxi', :action => 'list'

Step 15. Start the web application named app by issuing "mongrel_rails start -e production" in app directortory

Step 16. Open the internet explorer and access the following url


please refer to the following diagram:

Diagram 4. Result of accessing the http://localhost:3000/see

沒有留言:

發佈留言