Wednesday, July 27, 2005

Rails on Eclipse 3.1

I decided to create a review of the steps and setup I am currently utilizing to develop rails applications on Eclipse 3.1. There has been some discussion about the development of a full Rails implementation plugin on Eclipse, but for now, here is a basic setup to get things going.

Step 1 - Download and install the latest Eclipse IDE (3.1)

Step 2 - Download and install Rails on your machine

Rather than reproducing this portion of the setup, I would recommend following any of the well documented tutorials on this page.


Step 3 - Install the Ruby Development Tools Plugin for Eclipse

This is a plugin for developing in Ruby. It includes a Ruby Editor, Test::Unit Integration, Inherited Eclipse Features, Outline Views, Debugger, and Regular Expressions. It is full featured.

In Eclipse:
# Choose Help->Software Updates->Find and Install
# In the dialog choose "Search for new features to install" and push next
# Select "Add Update Site", enter an arbitrary name and the url http://rubyeclipse.sourceforge.net/nightlyBuild/updateSite/ (I recommend the Nightly Build since it has better support for Eclipse 3.1)
# Check RDT and push next
# Select the feature org.rubypeople.rdt and push next. RDT will be installed and be available after restart.


Be sure to configure the RDT Plugin in Eclipse after reboot (select Windows > Prefrences > Ruby (most importantly tell Eclipse where your local ruby installation lies - "Installed Interpreters" - Interpreter Name "Ruby 1.8.2" - Location "/usr/local/bin") and modify other settings as desired


Step 4 - Create a new Ruby Project in Eclipse (File > New > Other : Ruby > Ruby Project) with project name "Blog" (for this tutorial)
# make sure you are in the RDT perspective (Windows > Open Perspective > Other: Ruby)


Step 5
- Create a new Rails project from a command prompt (terminal window)

From the command line:
d'andrew% rails Blog


Step 6
- Import the newly created Rails Blog files/directory structure into your Eclipse Blog Project

From within Eclipse:
# right-mouse click on your Blog Project
# Select Import > File System
# Browse to your newly created Rails Blog folder and choose this folder
# Check the box next to the Blog Folder
# Under "Options" make sure "Create selected folder only" is checked (should be by default)
# Click finish


Step 7 - Run scripts from command line

Make sure that you are running the scripts from your eclipse workspace directory (when we imported the Rails Blog directory structure, eclipse copied it to your defined workspace)

From the command line ($PROJECT_ROOT):
d'andrew% ./script/server generate controller Blog


Step 8 - Refresh your Eclipse project (F5 for shortcut)

Step 9 and the rest
- Happy coding

Essentially the rest of it is composed of running scripts from the command line (although I am trying to figure out how to do this from within Eclipse) and refreshing your Eclipse project each time, then editing the files you need to.

Some other tricks/helps:
# To show all files in your Eclipse folder (ruby files are only shown by default in RDT plugin)
In the "Ruby Resources" box, there is a little down arrow at the far right. Click on this and un-click the "Show Ruby files only" option.
# Other plugins that are nice to have
CssEditor - for editing css files
XmlBuddy for editing XHTML files, etc.
JavaScript Editor Plugin


Naturally this is just a basic hack. If anyone has any good ideas I am anxious to hear them. And for the brave soul who wants to create the ultimate Rails plugin -- Happy Hacking!!!

16 comments:

Jeff said...

I could not get the unit tests to run under Eclipse 3.1. Were you able to run them yourself?

Kyle Heon said...

Thanks for this. I would be interested in learning how to run the Rails scripts from within Eclipse if you manage to find out how. Thanks again.

Kyle Heon said...

I also wasn't able to get the unit tests to run.

David Andrew Thompson said...

I'm trying to figure it out myself. I'll let you know as soons as I can. I'm also having trouble setting up the run routines, but such is life. ;)

Kyle Heon said...

The nightly build of RDT seems to fix unit testing. I'm still struggling with some configuration issues but I have managed to get unit tests to run from with Eclipse via RDT.

Rob Sanheim said...

Thanks for the tutorial - trackbacked here.

Brian Hogan said...

Great article! It helped me get going.

If you want to make the WEBRICK server start from Eclipse, you can do the following:

RUN | EXTERNAL TOOLS | EXTERNAL TOOLS
Click NEW, enter "Start Server" for the name

Location: c:\ruby\bin\ruby.exe or your system's path to ruby

Working directory:
${workspace_loc}\${project_path}

Arguments:
script\server

Save those settings and you should now be able to launch the webrick server from eclipse. It may not look like it worked, so do it again and you'll see the console appear with your messages.

Anonymous said...

This tutorial gives instructions for setting up the rails scripts for use inside Eclipse. I couldn't access it today, so you may want to use the cached version from Google or Yahoo!

Anonymous said...

I have setup "Start WEBrick" under external tools using "c:\ruby\bin\ruby.exe" as the location (yes, I have ruby installed), with the suggested working directory and arguments.

When I try to run it, I get the error: "C:\ruby\bin\ruby.exe: No such file or directory -- script/server (LoadError)"

However, I have verified that ruby is installed in this location. Any other suggestions on getting this working?

Anonymous said...

Starting WEBrick will only work from a rails project's root directory (that's where script/server.rb is). Thus, you need the to set the Working Dir to "${workspace_loc}\${project_path}" as mentioned above.

Anonymous said...

Do not select ant-build
but only run application...

Richstyles said...

You can also combine this with the RadRails plug in for all the models, views, controllers, scaffolds, and WEBrick.

http://download.radrails.org/update/

Parbir said...

Have you taken a look at EasyShell Eclipse Plugin. As the name suggests, its Easy to start a shell from within Eclipse and run all the Scripts to your heart's content.

http://pluginbox.sourceforge.net/

Have fun Coding

Ace said...

In kubuntu dapper, in a rails project, there is a directory called 'vendor' and it contains links.

One of those links is a link to the current directory.

When importing, links get dereferenced. This means that lot's of stuff from /usr/share/rails gets copied to the eclipse working directory. The directory rails, which points to the current directory, causes an infinite loop, the importing takes a long time, until eclipse sees the error of its ways and breaks off the import.

I solve it by deselecting the rails directory before importing.

Cheers
ace at suares dot an

ciscoblog said...

There is a resounding desire for meaningfull freeroll tournaments in which Online Casinos Extreme players at all levels have the opportunity to compete in tournament play when the prize money is huge... but the buyin is somewhat prohibitive most.

kubek2k said...

you can automate your rails app generation by adding an external tool similar to script/server

external_tool -> new program

location: {wherever_is}/rails

working dir: ${workspace_loc}${project_path}

args:
generate .

and it comes with generation of project in current directory with the same name.

It also solves that dirty links dereferencing problem - as a matter of fact it's a eclipse bug, and someone should do something about it ;/