10 Apr 2009

Understanding Scaffold....

Answer:

What is scaffold?
Scaffold is a way to quickly put an Active Record class online by providing a series of standardized actions for (CRUD) Creating, Reading/showing, Updating, and Destroying objects of the class. It is useful for quick prototyping. These standardized actions come with both controller logic and default templates that through introspection already know which fields to display and which input types to use. (Shin, 2009)

Now, I will use scaffold to generate Passenger ActionView, ActionController and ActiveModel. The steps are list as follow...

1. run "rails taxisys" to create another project. The Diagram-43 shows the result as follow.

Diagram-43 - taxisys taxi system

2. After that, taxisys is started at tcp port 5000 and the related screen dump is shown in Diagram-44.

Diagram-44 - taxtsys starts at tcp port 5000 with Mongrel (Instant Rails 2.0)

3. In order to make use of scaffold in ROR, the command "ruby script/generate scaffold Passenger name:string job_id:string contact_number:string suburb_origin:string street:string street_number:string building:string suburb_destination:string passenger_number:string taxi_type:string date:string time_required:string" is run and the diagram-45 shows that the Passenger Active Record (Model - app/models/passenger.rb), Action Controller (Controller - app/controllers/passengers_controller.rb) and Action View (View - app/views/passengers/*.html.erb) are generated.

Diagram-45 - Scaffold

4. After scaffold generates the Model, View and Controller for us, we can focus on the taxisys\db\migrate\001_create_passengers.rb file. This file contains the definition of the Passengers Class and its attribute. This file is used to create passenger table in mysql. The Diagram-46 shows the contents of the MVC files and the 001_create_passengers.rb file.

Diagram-46 - The contents of the files that scaffold generates

5. Before creating the table in mysql, we need to configure the config/database.yml and create taxisys database in mysql. The Diagram-47 shows that the content of database.yml file after configuration. The listing below shows the content of the database.yml file.

The content of the database.yml:
development:
adapter: mysql

database: taxisys_development
username: root
password:
host: localhost
test:
adapter: mysql
database: taxisys_test
username: root
password:
host:
localhost
production:
adapter: mysql
database: taxisys_production
username: root
password:
host: localhost


Diagram-47 - the taxisys_test, taxisys_development and taxisys_production are created

6. run "set RAILS_ENV=development" command first and then run "rake db:migrate" command to create passengers table in taxisys_development database. The Diagram-48 shows the commands' screen dump. In the screen dump, passengers table is shown and create by "rake".

Diagram-48 - the rake command output

7. After All, scaffold is finished. We can open IE browser with url - http://localhost:5000/passengers to interact with the Action View. We can list the record in passengers table. We can update (edit) specific record of the table. We can drop the table. And also we can create a new record in the table. The Diagram-49 shows the Action View in IE browser.








Diagram-49 - Action View

It is the scaffold in ROR and it helps to build application very fast.

Reference:

1. Shin Sang (2009). "EMPOWER:YOU". Sun Tech Days 2008-2009, Retrieved from URL - http://www.suntechdays2008.com/down/1015/track1_java/T1S6_jrubyrails_sangshin.pdf


No comments:

Post a Comment