顯示包含「Workshop 2」標籤的文章。顯示所有文章
顯示包含「Workshop 2」標籤的文章。顯示所有文章

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

4/06/2009

Workshop 2 -Part1

To Do:

1. Set up a focus group (like a study group for peer learning) to work on the Ruby on Rails workshops via Interact tools as a class.

Join the focus group at http://railsfocusgroup.blogspot.com/


2. What is meant by “convention over configuration” and how does it reduce coding?

The below Information from Beginning Rails(2007) at http://books.google.com.hk/books?id=0rD_q96srHoC&pg=PA356&dq=%22convention+over+configuration%22+and+%22coding%22&lr=#PPA7,M1.

Convention over configuration means that the programmer needs to define only configuration that is unconventional. Programming is all about making decisions. If you were to write a system from scratch, without the aid of Rails, you would have a lot of decisions to make: how to organize your files, what naming conventions to adopt, and how to handle database access are only a few. If you decided to use a database abstraction layer, you would need to sit down and write it, or at least find an open source implementation that suits your needs. You would need to do all this before you even got down to the business of modeling your domain. Rails lets you get started right away, by encompassing a set of intelligent decisions about how your program should work, alleviating the amount of low-level decision making you need to do up front. As a result, you can focus on the problems you're tying to solve and get the job done quicker.

Rails ships with almost on configuration files. If you're used to other framework, this fact might surprise you. If you’ve never used a framework before, you should be surprised. In some causes, configuring a framework is nearly half the work. Instead of configuration, Rails relies on common structures and naming conventions, all of which employ the often-cited principle of least surprise (POLS). Things behave in a predictable, easy-to-decipher way. There are intelligent defaults for nearly every aspect of the framework, relieving you, the developer, from having to explicitly tell the framework how to behave. This isn’t to say that you can’t tell Rails how to behave. In fact, most behaviors can be customized to your liking and to suit your particular needs. While you can manipulate most things in the Rails setup and environment, the more you accept the defaults, the faster you can develop applications and predict how they will work. The speed with which you can develop without having to do any explicit configuration is one of the key reasons why Rails works so well.

3. Further work on understanding MVC:
a. See the wiki at

http://wiki.rubyonrails.org/rails/pages/UnderstandingMVC - Done!
b. Do the MVC tutorial at http://wiki.squeak.org/squeak/1767 - Done! (Try to access the web page at http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html, however, this website has been obsoluted.)

4. Got a spare hour or so? I recommend the UC Berkeley RAD lab’s Ruby on Rails Short course at
http://youtube.com/watch?v=LADHwoN2LMM - Done!


5. Read the Flash article using ActionScript by Colin Moock titled “The Model-View-Controller Design Pattern “at
http://www.adobe.com/devnet/flash/articles/mv_controller.html - Done!