Make Ruby on Rails easy with RadRails and Eclipse
Ruby on Rails has hit the big time. With that popularity has come the desire by developers for an integrated development environment (IDE) that makes Rails even more accessible. RadRails, a Eclipse-based environment, fulfills that need for many developers.
This article introduces the RadRails IDE. We assume you already know how to develop Web applications with Ruby on Rails, so we won't spend any time explaining Ruby or Rails code and will instead focus on the IDE itself.
Downloading and installing RadRails
So you want to jump on the Ruby on Rails bandwagon, but you're not ready to give up the comfort of an IDE. Or maybe you've been doing Rails stuff for a while and now you want to move past the editor you've been creaking along in. Whatever is giving you the urge to get going with RadRails, the good news is that RadRails is easy to install and use. This section starts with installation; the rest covers using RadRails.
RadRails doesn't require much in the way of prerequisites. It will run on any Microsoft® Windows®, Linux®, or Mac OS X system with Ruby V1.8.4, Rails V1.1 or later, and Java™ V1.4 or later.
If your system meets these requirements, grab a copy of RadRails from the project's Web site (see Related topics). You also can find links to Eclipse plug-ins to run RadRails from within your existing Eclipse installation and the source code. Note: Working with either of these distributions is beyond the scope of this article.
Once you've downloaded the appropriate zip file, just extract it and run
the radrails executable. You may need to tweak your
make sure RadRails can find your Java installation.
Starting and using RadRails
Once RadRails is installed, you can dive into using it. This section takes a quick look at the RadRails application and navigating within and between projects. There are several ways you can start working with a project, including starting a new project and importing an existing project.
Whichever route you choose to follow, getting started is the most important part. The best way to learn RadRails is to jump in and use it. But take this quick tour before you make a leap.
The RadRails application
Our tour begins with a look at the RadRails application containing a single project. You'll learn how to add a project in just a bit.
Figure 1 shows the RadRails application. Actually, it shows the Rails "perspective" of the application, but don't worry about that little nit. You can think of it as a window divided into six areas: the Menu bar, the Tool bar, the Navigator and Test::Unit pane, the Editor pane, the Outline pane, and the External Tools pane.
Figure 1. The RadRails application
- Menu bar
- The Menu bar is at the very top of the application window. It behaves like menu bars in almost every other application. One important menu item is Window > Preferences, which lets you set or select your Ruby interpreter. If you don't do this, trying to run many of the scripts Rails that creates will fail (the unit-test scripts, for example).
- Tool bar
- The Tool bar is just beneath the Menu bar. Again, it's similar in behavior to toolbars everywhere. The button on the far left is the New button.
- Navigator and Test::Unit pane
- The Navigator and Test::Unit pane takes up the left side of the application window. Navigation in and between projects will be discussed shortly. The Test::Unit portion allows you to monitor your unit-testing status.
- Editor pane
- The Editor pane is in the center of the application window. The editor is a nice editing widget that provides syntax coloring, indenting, and other tools. Right-clicking in the editor brings up a menu with many other tools, including debugging options, a mechanism for running the code in the file, commenting and uncommenting selected regions, etc.
- Outline pane
- The Outline pane is on the right side of the application window. This pane provides an outline-styled view of the source file currently being edited. This view allows for quick navigation to methods and classes within the file.
- External Tools pane
- The External Tools pane is at the bottom and on the right of the application window. This pane provides tabbed access to Web server controls, generators, a RegExp tool, and other tools.
Navigating within and between projects
The Navigation pane holds an expandable and collapsible hierarchy of projects that exist in the Workspace. Clicking a project expands the directories and files that make up the project. (Figure 2 shows a RadRails window with an expanded project in the Navigation pane.)
Figure 2. An expanded project
As long as the project is highlighted, it's the active project for all other functions in RadRails. This lets you run generators, start Web servers, etc.
Double-clicking a file opens the file in an editor in the Editor pane. Opening multiple files opens multiple files in tabs. Figure 3 shows four files open in the Editor pane.
Figure 3. Multiple files in tabs
Starting a new project
Starting a new project is easy. Click New on the Tool bar, or select File > New from the Menu bar. Either one brings up the New dialog box. This box lets you select the wizard appropriate to the kind of project you're starting. Options include Web server configurations, SQL files, Ruby projects and classes, and Rails projects (the only option we look at here).
Select Rails > Rails Project and click Next. This takes you to a new screen, where you enter the project name. The default values for the other options should be fine for experimentation, but you may want to adjust them later:
- Use default location (set to a directory named the same as your project in your Workspace directory)
- Generate Rails application skeleton (set to true)
- Create a WEBrick server (also set to true)
- Disable table pluralization (set to false)
After making any needed changes, click Finish. Doing so creates the project, which makes it visible in the Rails Navigator pane on the left side of the RadRails interface.
Importing an existing project
If you've already got a Rails project and you'd rather use it than start anew, there's a simple way to import it. Start by clicking New on the Tool bar or selecting File > New from the Menu bar, just as you would to start a new project. Select Rails > Rails Project in the dialog box, and click Next. After naming your project, click the Generate Rails application skeleton check box to turn off that option. Then click Finish.
Once the project appears in the Rails Navigator pane, you can right-click it to pull up a menu of options. Select Import from this menu, and an Import dialog box pops up. Select General > File System and click Next. Doing so lets you select the source to import. In addition to selecting the source, you have options for the folder you want to import into (this defaults to the project's workspace), whether to overwrite existing files (defaults to no), whether to create any additional needed files or folders (defaults to false), and limiting your import to just the folders you select (defaults to true).
Once your chosen directory appears in the file-browsing windows, you can select it (the error message "There are no resources currently selected for import" appears at the top of the dialog box until you do select something), then click Finish to import the files. If any duplicates exist, an additional dialog box appears, asking whether you want to overwrite the original with the imported version.
Building an application in RadRails
Begin your project by creating the database it will run against. The best way to do this is on the command line: Use a generator to create a migration script, then edit the script. You'll follow the same pattern in RadRails.
Make sure you have an active project selected by clicking it (or one of the resources that belongs to it) in the Navigation pane. Then click the Generators tab in the External Tools pane to bring up the Generator tool, as shown in Figure 4. Select the Migration generator from the pull-down menu at upper left in the pane. To run the generator, click Go in the upper-right corner.
Figure 4. The Generator tool
Clicking Go opens a terminal widget in the Editor pane and runs the generator script in it, showing any output from the command there. You can then expand the db > migrate branch of your project tree in the Navigator pane and click the filename to open the file in an editor in the Editor pane (see Figure 5). Once you've edited the migration script to meet your needs, you can run it from Rake and begin working on the application.
Figure 5. Editing the migration
Once again, you'll fall back on the generators and work from there. This
time, select the Scaffold generator and give it the name
Example Admin in the Options field (the center field, between
the pull-down menu and the Go button). Click
Go to run the generator in the terminal widget.
At this point, you have enough of an application that you can run the Web server and see that everything works. Click the Servers tab in the External Tools pane, click the appropriate server from the list that appears in the pane, then click Start (a green arrow, second from the left on the small toolbar at the top of the External Tools pane). This starts the Web server and a console for it. You can access this console by clicking the (now bold) Console tab in the External Tools pane (see Figure 6).
Figure 6. The Console tab
Because the generated scaffolding includes unit tests, you can run those.
You did set your Ruby interpreter earlier, when it was discussed, didn't
you? The default tests will pass (they only assert that true is true), but
you can force a failure to see what happens. Edit the unit-test script by
choosing Test > Unit > example_test.rb in the
Navigator pane and change the assertion in the
method to read
assert false. Now you know it will fail.
To run the tests, click Run Unit Tests in the Tool bar (the third button from the left in the third group of buttons from the left). Because the tests fail, the green bar near the top of the Test::Unit tab of the Navigation pane turns red, a list of failures appears in the Failures tab of the middle field of the pane (in this case, just the one), and a stack trace for the selected failure appears in the Failure Trace field of the pane. (If you're really observant, you'll also note that the title space of the tab shows the run time for the unit tests.)
Click the Hierarchy tab of the middle field of the Test::Unit pane to see a tree view of all the failures from the current run. Selecting a failure in this tab (or the Failures tab) also selects the same failure in all the other tabs in the Test::Unit pane.
Run the integration or functional tests by clicking their respective buttons on the Tool bar (or all the tests, with the Run All Tests button). Doing so runs the appropriate suite of tests and provides access to the results through the Test::Unit pane in much the same way.
This article provides a high-level walk through the RadRails IDE for Ruby on Rails. It looks at installing RadRails, the structure and use of the application window, and the primary steps in developing an application. The next steps are up to you. Once you've downloaded and installed RadRails and worked through the steps shown here, you know enough to begin using RadRails for your own development.
- The primary reference for RadRails is RadRails.org.
- Get Java technology from Sun Microsystems or from IBM.
- For an excellent introduction to the Eclipse platform, see "Getting started with the Eclipse Platform."
- Learn more about the Eclipse Foundation and its many projects.
- Be sure to read Ruby on Rails: Up and Running.
- Check out the latest Eclipse technology downloads at IBM alphaWorks.
- Expand your Eclipse skills by visiting IBM developerWorks' Eclipse project resources.
- Browse all of the Eclipse content on developerWorks.
- Innovate your next open source development project with IBM trial software, available for download or on DVD.