advertisement

Print

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

Pages: 1, 2, 3, 4, 5

Next Pagearrow