Saturday, September 5, 2009

Workshop 3

1. Set up the MySQL tools on your computer
A MySQL tool provides a user interface for the MySQL database, as opposed to using the command line editor for CRUD (Create, Retrieve, Update and Delete) database operations.
Screen shoot:























2. Rails will setup a new application directory for each of your Web application projects. Get InstantRails (Windows) or Locomotive (MacOS) running on your machine. Both packages install Ruby, Rails, a Web server or one called ‘Mongrel’ or another small Ruby Web server called ‘WEBrick’, and MySQL “inside a bubble” as I call it so that others parts of your system are not modified (Similarly ZOPE does with installing its own Web server and Python versions).

In the Workshop 2, I demonstrated how to setup the Model component, the Controller component and the View Component using the Rails framework, I therefore had already installed and tested the environment. The following screen shots and command line entries describe the process of running the default web server “Mongrel”.

Move to the directory, in this case “taxi” and then enter:

cd taxi
ruby script/server

The following is a screen shot of the process:

















If you want to use the other webserver “WEBrick” you would have to type:

ruby script/server webrick

To view the web server, you have to type, the following into your browser address bar:

http://localhost:3000/
























3. Once Rails is running for you at http://localhost:3000, you need to configure database access. Connection to the database is specified in the config/database.yml file.

Here is a screen shot of the initial default config/database.yml file contents:






















The easier way to setup the MySQL database is to use the Rails command with an override flag of “-d mysql”, like this:

cd ..
rails taxi -d mysql

This regenerates your whole rails project without having to make modifications. The only change is then to check the config/database.yml and make sure you have the correct password setup.

The following screen shots show how the project gets regenerated and the changes are added to the config/database.yml file:




















You have to use the rake command to create the databases specified in the config/database.yml file, this is shown here:










Finally if we view the MySQL Adminstrator console, we will see that the databases have been created:


















4. Generate the Passenger model by creating the MySQL database and ‘passengers’ table from the information above.

Using the View component you can generate the scaffolding for the passengers table:


ruby script/generate scaffold passenger name:string contact_number:string suburb_origin:string street:string street_number:string building:string suburb_destination:string passenger_number:string taxi_type:string date:datetime time_required:datetime


Or alternatively if you do not want all of the CRUD functionlity, or just want a table you would use the Model component:


ruby script/generate model passenger name:string contact_number:string suburb_origin:string street:string street_number:string building:string suburb_destination:string passenger_number:string taxi_type:string date:datetime time_required:datetime








Then using the rake command setup the passengers table in MySQL:


rake db:migrate









Finally you can view the database and table structure in MySQL:
















One of the anomalies of the Convention over Configuration (CoC) paradigm I find a little frustrating is how do you undo something if you don’t understand the configuration of the Rails framework? Normally you approach a programming task with some experience, in CoC, the approach is to rely on the framework to shield you from extraneous detail, this however means that if you make a mistake you you are left with a broken system.





No comments:

Post a Comment