O'Reilly    
 Published on O'Reilly (http://oreilly.com/)
 See this if you're having trouble printing code examples


Ruby on Rails Meets Eclipse

by Deepak Vohra
04/26/2007

Eclipse is the most commonly used Java IDE. Ruby on Rails provides some plugins to enable its use in Eclipse. Two such plugins are Ruby Development Tools and RadRails. Ruby Development Tools (RDT) and RadRails are open source Ruby IDEs for the Eclipse platform. Some of the features of RDT are syntax highlighting, syntax validation, error markers, code completion, code formatting, and Ruby unit-testing framework integration. RadRails features include code assist on Ruby and RHTML files, debugging support, integrated server views, generators, dynamic testing, and integrated deployment. In this article, we'll develop a Ruby on Rails Create-Read-Update-Delete (CRUD) application in Eclipse under Windows. If you haven't already installed Eclipse, download and install Eclipse 3.2. Also install the MySQL database if it is not already installed.

Installing Ruby on Rails

First, we need to install Ruby on Rails. In this section we'll install Ruby, RubyGems, and Rails. RubyGems is the standard Ruby package manager used with Ruby applications and libraries. Download the Ruby Windows Installer application. Double-click on the ruby184-21.exe application. Ruby Setup Wizard will start. Click on Next, accept the license agreement, and click on Next again. Select the default components to install, which include the RubyGems package manager and SciTE, a Scintilla-based text editor, and click on Next. Specify a directory to install Ruby (the default is c:/ruby) and click on Next. Specify a start folder and click on Install. Ruby and RubyGems will be installed. Click on Finish to close the Ruby Setup wizard, and Ruby is installed. The directory path c:/ruby/bin gets added to the system environment variable PATH. The system environment variable RUBYOPT with value -rubygems also gets added.

In a command-line window, cd (change directory) to the c:/ruby directory and run the following command to install Rails and dependencies, including activerecord and actionpack (activerecord implements the model layer of a Rails MVC application and actionpack implements the view and controller):

c:/ruby>gem install rails --include-dependencies

The Rails framework and dependencies are installed. Next, we'll discuss the Ruby Development Tools (RDT) plugin.

Installing RDT

To install Ruby Development Tools, select Help>Software Updates>Find and Install in the Eclipse IDE. In the Features Updates frame, select "Search for new features to install" and click on Next. In the Install frame, click on the "New Remote Site" button. In the New Update Site frame, specify a Name (RDT, for example), and in the URL field specify http://updatesite.rubypeople.org/release. Click on the OK button. In the "Update sites to visit" frame, select RDT and click on Finish. Select the features to install, select the checkbox "Select the latest version of a feature only", and click on Next.

Installing RDT
Figure 1. Installing RDT

Accept the feature license agreement and click on Next. In the Installation frame, select the Ruby Development Tools feature and click on Finish. In the Feature Verification frame, click on Install All. Ruby Development Tools and the plugin will be installed. Restart Eclipse for the configuration changes to take effect. Next, we need to configure the Ruby preferences. Select Window>Preferences. In the Preferences frame, select Ruby>Installed Interpreters. In the Add RubyVM frame, specify an RubyVM name--Ruby, for example--and in the RubyVM home directory field, specify the location of the rubyw.exe application. If Ruby on Rails is installed in the c:/ruby directory, rubyw is in the c:/ruby/bin directory. Click the OK button. Click the OK button in the Preferences frame.

Creating a Rails Project

Next, we create a new Ruby project in Eclipse. Select File>New>Project. In the New frame, select the Ruby Project wizard and click on Next.

Creating a Ruby Project
Figure 2. Creating a Ruby project

In the Ruby Project frame, specify a Project Name and click on Finish. Open the Ruby perspective if it is not already open. A new Ruby project gets added to the Eclipse IDE. Next we need to configure external tools for various tasks such as creating a Rails application, creating a Model, creating a Controller, creating a scaffold, and starting the WEBrick server. Select Run>External Tools>External Tools to create an external tools configuration. In the External Tools frame, create configurations for various tasks. To create a configuration, right-click on the Program node and select New. Create a configuration, "Create Rails Application," to create a Rails application. In the Location field, specify the rails.cmd file. In the Working Directory field, select the variable ${project_loc}. To create an application by the same name as the Rails project, specify ../${project_name} in the Arguments text area. Click on the Apply button to apply the configuration.

Creating Rails Application Configuration
Figure 3. Creating a Rails application configuration

Because the Rails application generates some directories and classes, we need to refresh the project resources. Select the Refresh tab. In the Refresh frame, select the checkbox "Refresh resources upon completion." Click on the Apply button. To add the configuration to favorites, select the Common tab and select the External Tools checkbox in the "Display in favorites" menu. Click on the Apply button. Next, create a configuration to start the WEBrick server. In the Location field, specify the ruby.exe application; in the Working directory frame, specify ${project_loc}; and in the Arguments text area, specify script/server. Click on the Apply button to apply the configuration.

Start WEBrick Configuration
Figure 4. Start WEBrick configuration

Similarly, create configurations to create a Ruby model script, create a controller script, create a scaffolding, and run the rake command. To create a model script, specify ruby.exe in the Location field, specify ${project_loc} in the Working Directory field, and specify script/generate model {string_prompt: Model name} -f in the Arguments text area.

Create Model Configuration
Figure 5. Create model configuration

To create a controller script, create a configuration, "Create Controller." Specify ruby.exe in the Location field, ${project_loc} in the Working Directory field, and script/generate controller {string_prompt:Controller name} –f in the Arguments text area.

Create Controller Configuration
Figure 6. Create controller configuration

We also need to create a configuration, "Create Scaffold," for creating scaffolding classes. Specify ruby.exe in the Location field, ${project_loc} in the Working Directory field, and script/generate scaffold {string_prompt:Model name} {:string_prompt:Controller name} –f in Arguments.

Create Scaffold Configuration
Figure 7. Create scaffold configuration

We can now create an example Rails application, and a CRUD application to create a catalog entry, read a catalog entry, update a catalog entry, and delete a catalog entry. To create a Rails application, select the Ruby project "catalog" in the Ruby Resources view, and select Run>External Tools>Create Rails Application. A Rails application, catalog, gets created with the directory structure shown in Figure 8.

Rails Application Directory Structure
Figure 8. Rails application directory structure

In the next section, we'll create a MySQL database table using Rails migrations.

Creating a Database Table

First, we need to modify the database.yml file for the MySQL database. Modify the development configuration with the following settings, as shown in Figure 9.

development:
  adapter: mysql
  database: test
  username: root
  password: 
  host: localhost

Modifying database.yml
Figure 9. Modifying database.yml

Next, we will create a database table in the MySQL database using migrations, for which we need to create an external tools configuration for the rake command. Rake is similar to Java's ant command and is used to run the migrate target. Specify rake.bat in the Location field of the Rake configuration. Specify {project_loc} in the Working Directory field and migrate in Arguments.

Rake Configuration
Figure 10. Rake configuration

We will create a migration script by creating a model script, which also creates a migration script. Select Run>External Tools>Create Model to create a model script. In the Variable Input frame, specify catalog as the model name and click on OK. A model script, catalog.rb, and a migration script, 001_create_catalogs.rb, get added to the Rails project catalog. Modify the migration script to create a database table, "catalogs." Migration script 001_create_catalogs.rb is listed as follows:

class CreateCatalogs < ActiveRecord::Migration
  def self.up
    create_table :catalogs do |t|
   t.column :journal, :string, :limit => 255
   t.column :publisher, :string, :limit => 255
   t.column :edition, :string, :limit => 255
    t.column :title, :string, :limit => 255
   t.column :author, :string, :limit => 255

end

Catalog.create :journal => "developerWorks", 
:publisher => "IBM", :edition => "September 2006", 
:title=> "A PHP V5 migration guide",:author=>"Jack D. Herrington"
Catalog.create :journal => "developerWorks", 
:publisher => "IBM", :edition => "September 2006", 
:title=> "Make Ruby on Rails easy with RadRails and Eclipse",
:author=>"Pat Eyler"
   
  end
  def self.down
    drop_table :catalogs
  end
end

Start the MySQL database, if it is not already started, and run the migration with the rake command. Select the migration script and select Run>External Tools>Rake. The database table "catalogs" gets created, as shown in Figure 11.

Creating Database Table
Figure 11. Creating a database table

Creating a CRUD Application

Next, we will create a CRUD application with the scaffold generator. The scaffold generator generates all the required model and controller scripts and the RHTML view templates for an interface to data in a database table. Select Run>External Tools>Create Scaffold to create a scaffolding for the database table catalogs. Specify a model name, catalog, for example, and a controller name, catalog, in the Variable Input frames. A scaffolding, which consists of a model class, catalog.rb; a controller class, catalog_controller.rb; and view templates, _form.rhtml, edit.rhtml, show,rhtml, list.rhtml, and new.rhtml, gets created in the Rails application. Start the WEBrick server with Run>External Tools>Start WEBrick server.

Starting WEBrick Server
Figure 12. Starting WEBrick server

Access the WEBrick server with the URL http://localhost:3000.

Rails index.html Page
Figure 13. Rails index.html page

The catalog entries listing can be displayed with the list controller action. Invoke the controller action list with the URL http://localhost:3000/catalog/list.

Catalog Listings
Figure 14. Catalog listings

Next, we'll discuss the RadRails plugin.

Installing RadRails

Installing the RadRails plugin requires the RDT plugin to be installed. Therefore, if you have not installed the RDT plugin, as explained in the previous section, do so now. To install the RadRails plugin, select Help>Software Updates>Find and Install. Select "Search for new features to install" in the Feature Updates frame and click on Next. Click on the New Remote Site button in the "Update sites to visit" frame. Specify a update site name--RadRails, for example--and specify the URL http://radrails.sourceforge.net/update in the URL field. Click the OK button. Select the RadRails update site configuration and click on Finish. Select the features to install and click on Next. Accept the feature license and click on Next. In the Installation frame, click on the Finish button. Select "Install All" in the Feature Verification frame. Restart Eclipse for the plugin configuration to take effect. In the Eclipse IDE, open the RadRails perspective by selecting Window>Open Perspective>Other>Rails.

Opening the Rails Perspective
Figure 15. Opening the Rails perspective

The RadRails perspective includes the following views: Servers, Generators, Rake Tasks, Console, RI, and Rails Plugins. The Servers view consists of WEBrick server configurations for the Rails applications. The Generators view consists of generators for model, controller, scaffold, and migration scripts. The Rake Tasks view is still in development and consists of rake tasks such as migrate.

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.

Creating a Rails Project
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.

Rails Application
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

Modifying database.yml
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.

Creating Scaffolding for 'catalogs' table
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, _form.rhtml, show.rhtml, list.rhtml, edit.rhtml, and 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.

Starting WEBrick Server
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.

Listing catalogs
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.

Copyright © 2009 O'Reilly Media, Inc.