Instant Rails with NetBeans
Two weeks ago I introduced myself to Ruby on Rails, which was, technically, JRuby on Rails. Based on some constructive comments by David Rupp, I've spent some time over the past week doing some additional research on rails. In the process I began to look at pure Ruby (not JRuby).
Now, I'm sorry, this blog entry is somewhat Windows biased, which feels odd for a guy that normally talks about Java. However, playing with Ruby for the past week I realized the easiest way to get up and running with Rails on Windows is to use Instant Rails, which contains everything you need in one nice package: Ruby, Rails, Mongrel and MySQL.
Setting Things Up
- Download and extract Instant Rails.
- Run InstantRails.exe. The first time InstantRails.exe runs, it will want to update the paths in the Rails configuration files to the location you extracted the archive. InstantRails starts Apache (which you can stop) and MySQL.
- Create an environment variable named RUBY_HOME that points to the Ruby location within Instant Rails. For example: C:\Software\Ruby\InstantRails-1.6-win\InstantRails\ruby.
- Add %RUBY_HOME%\bin to your Path environment variable.
- Download and install NetBeans 6.0 M7 or greater.
- Download and save the MySQL JDBC driver somewhere on your workstation. We need this to use the NetBeans Database tools with MySQL.
- Start NetBeans and switch to the Runtime tab (Ctrl+5).
- Expand the Databases > Drivers node and right-click to add a new driver.
- Browse to the location where you saved the MySQL JDBC driver. NetBeans will detect the driver class and recommend a name:
- Right-click the MySQL (Connector/J driver) now listed under Drivers and select Connect Using. Instant Rails comes with a sample database called cookbook, that we can use to test our connection:
- If all goes well, you should be able see your connection and browse the database:
- Install the Ruby module from the update center (found in the Features folder. Many dependant modules will come along). The Ruby module requires a restart of the IDE.
- The Ruby module also installs JRuby and Rails. However, we're going to point NetBeans at the location where Instant Rails installed Ruby. Open the Options dialog Under Miscellaneous, expand the Ruby Installation node and point NetBeans to the version of these files found in instant Rails installation
Test the Installation
Instant Rails comes with a sample cookbook application which we'll set up in NetBeans. This will also serve as an example of how to set up existing rails projects you already have in NetBeans.
- Create a new Ruby on Rails Project From Existing Sources.
- Browse to your Instant Rails installation directory where you'll find a rails_apps folder. Select the cookbook folder under rails_apps and click Finish.
- The project will open in NetBeans and the Mongrel server will be started (The Output window says WEBrick, but it's actually Mongrel that's running. Also, the server is started even though the Output window just says STARTING. This issue has been logged.).
- Try the following: http://127.0.0.1:3000 and you should see the Online Cookbook.
- If you run into problems, check that you can verify the Instant Rails setup from the command line.
Create Your Own Rails Project
- Create a new Ruby on Rails Application project and click Finish.
- Right-click the Controllers folder and select Generate. Name the controller MyTest. NetBeans will run the script to generate the controller.
- Open the MyTestController class and add the following:
- Press F6 to run the project and browse to http://127.0.0.1:3000/My_Test. You get a Routing Error.
This is because the Mongrel (and WEBrick for that matter) web server is only capable of handling one application at a time. It needs to be restarted when we switch to another application.
- Since we're letting NetBeans start the server, the only way I know to kill it is to kill the ruby.exe process using the Windows Task Manager. Kill ruby.exe, press F6 to run the project, and browse to http://127.0.0.1:3000/My_Test.
Another Way To Run Mongrel
Since NetBeans doesn't yet provide the best integration with Mongrel, you may prefer to run it from the command line. Let's see how this works.
- First, kill the ruby.exe again started by NetBeans.
- Open the command prompt to the location of your Rails application above. It should be named RailsApplication1 if you accepted the default name.
- Type ruby script/server (if ruby isn't found, make sure you set up your path correctly as described under setup above).
Now you see there's at least a cleaner way to stop the server.
- To run the cookbook application, CTRL-C to stop mongrel, switch to the cookbook directory and run ruby script/server again.