Skip to main content

Instant Rails with NetBeans

Posted by bleonard on March 19, 2007 at 11:24 AM PDT

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

Instant Rails

  1. Download and extract Instant Rails.
  2. 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.






  3. 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.
  4. Add %RUBY_HOME%\bin to your Path environment variable.

NetBeans

  1. Download and install NetBeans 6.0 M7 or greater.
  2. Download and save the MySQL JDBC driver somewhere on your workstation. We need this to use the NetBeans Database tools with MySQL.
  3. Start NetBeans and switch to the Runtime tab (Ctrl+5).
  4. Expand the Databases > Drivers node and right-click to add a new driver.
  5. Browse to the location where you saved the MySQL JDBC driver. NetBeans will detect the driver class and recommend a name:







    Click OK.
  6. 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:






  7. If all goes well, you should be able see your connection and browse the database:








  8. 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.
  9. 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.

  1. Create a new Ruby on Rails Project From Existing Sources.
  2. 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.
  3. 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.).
  4. Try the following: http://127.0.0.1:3000 and you should see the Online Cookbook.




  5. If you run into problems, check that you can verify the Instant Rails setup from the command line.

Create Your Own Rails Project

  1. Create a new Ruby on Rails Application project and click Finish.
  2. Right-click the Controllers folder and select Generate. Name the controller MyTest. NetBeans will run the script to generate the controller.
  3. Open the MyTestController class and add the following:




  4. 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.
  5. 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.

  1. First, kill the ruby.exe again started by NetBeans.
  2. Open the command prompt to the location of your Rails application above. It should be named RailsApplication1 if you accepted the default name.
  3. 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.
  4. To run the cookbook application, CTRL-C to stop mongrel, switch to the cookbook directory and run ruby script/server again.
Related Topics >>