Creating a Rails Application
Next, we create a Rails project with File>New>Project. In the New gallery, select the Rails Project wizard and click on Next.
Figure 16. Creating a Rails project
Specify a project name, catalog, in the New Rails Project frame and select the options "Generate Rails Application skeleton" and "Create a WEBrick Server." Click on Finish. A Rails application gets created, as shown in the Rails Navigator view.
Figure 17. Rails application
We need to modify the database.yml file for the MySQL database:
development: adapter: mysql database: test username: root password: host: localhost
Figure 18. Modifying database.yml
Create a database table,
catalogs, in the MySQL database using SQL script:
CREATE TABLE catalogs(Journal VARCHAR(255), Publisher VARCHAR(255),Edition VARCHAR(255), Title Varchar(255), Author Varchar(255)); INSERT INTO catalogs VALUES('developerWorks', 'IBM', 'September 2006', 'A PHP V5 migration guide', 'Jack D. Herrington'); INSERT INTO catalogs VALUES('developerWorks', 'IBM', 'September 2006', 'Make Ruby on Rails easy with RadRails and Eclipse ', 'Pat Eyler');
Creating a CRUD Application
Next, we create the scaffolding for the database table
catalogs. ActiveRecord uses pluralization; the database table name is the plural of the model class name with the first letter of the table name in lowercase. The scaffolding consists of model and controller scripts and RHTML view templates. Create the scaffolding with the scaffold generator. Select the Generators view and select the scaffold generator. Specify a model name,
catalog, and click the Go button. A controller name is optional in the scaffold generator and defaults to the plural of the model name.
Figure 19. Creating scaffolding for the "catalogs" table
The scaffolding for the
catalogs table gets created. The scaffolding consists of the model class,
Catalog, which extends the
ActiveRecord::Base class, and a controller class,
CatalogsController, which extends the
ApplicationController class, which in turn further extends the
ActionController::Base class. The model script is generated in the app/models directory, and the controller script is generated in the app/controllers directory. The view templates,
new.rhtml get created in the app/views/catalogs directory. Next, we start the WEBrick server configuration for the Rails application,
catalog, as shown in Figure 20.
Figure 20. Starting WEBrick server
The WEBrick server gets started. Display the WEBrick console with the URL http://localhost:3000. Display the list of the catalog entries with the controller action
list. Invoke the
list action with the URL http://localhost:3000/catalogs/list.
Figure 21. Listing catalogs
As you can see, a Ruby on Rails application may be created in a command-line tool, but it can also be created in Eclipse using one of the Ruby on Rails plugins for Eclipse.
Deepak Vohra is a NuBean consultant and a web developer.
Return to Ruby.