Friday, November 7, 2008

JRuby on Rails (JRoR) Development with IntelliJ IDea 7.0.4

The following was done with IDea 7.0.4 and JRuby 1.1.5. The following is a JRoR/JRuby on Rails Tutorial to get setup with IntelliJ IDea 7.0.4, The IDea Ruby Plugin, JRuby on Rails and to create a Hello World application. Install JRuby SDK: * Download from * Setup a symbolic link to directory called jruby-(version) called jruby (so you can just change symlink to a new version later without having to change project configs)
cd (parent dir containing jruby-(version) dir)
ln -s jruby-(version)/ jruby
* Setup a JRUBY_HOME environment variable and put (path)/jruby/bin in the path add the following to ~/.bash_profile
export JRUBY_HOME=/replace/with/path/to/jruby
Add support for environment variables in IDea, etc. * We add key/string pairs to ~/.MacOSX/environment.plist
mkdir ~/.MacOSX/
* Create/edit ~/.MacOSX/environment.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "">
<plist version="1.0">
        <!-- the file format is just key, string, key, string, with no container element around each pair -->
        <!-- ... more env vars can go here -->
* Logout and login after creating that file/making changes to it. Installing Rails, Rake, ActiveRecord Gems and JDBC Driver Jars Note: For the following install commands, if you're curious about what --no-rdoc --no-ri means... --no-rdoc means do NOT generate RDoc documentation for the gem on install --no-ri means do NOT generate RI documentation for the gem on install * Install Rails gem
jruby -S gem install rails -y --no-rdoc --no-ri
DB stuff MySQL Active Record Support * You probably *don't* need to copy the JDBC driver jar (for example: mysql-connector-java-5.0.5-bin.jar) to $JRUBY_HOME/lib since it is included in the activerecord-jdbcmysql-adapter gem, although it is a somewhat old jar; activerecord-jdbcmysql-adapter-0.8.2 included ./lib/ruby/gems/1.8/gems/jdbc-mysql-5.0.4/lib/mysql-connector-java-5.0.4-bin.jar). * Install the following gems
jruby -S gem install activerecord-jdbc-adapter --no-rdoc --no-ri
jruby -S gem install activerecord-jdbcmysql-adapter --no-rdoc --no-ri
(note: in one of the cited references it was called activerecord-jdbc which is wrong. Thanks Jim and Richard for that info.) Oracle Active Record Support * Copy the Oracle JDBC driver jar(s) for your DB to $JRUBY_HOME/lib (for example: ojdbc14.jar) * Install the following gem
jruby -S gem install activerecord-jdbc-adapter --no-rdoc --no-ri
* Add require 'ojdbc14' to config/environment.rb (thanks Richard!) * Install the Rake gem (Rake is a build tool like Ant/Maven for Ruby)
jruby -S gem install rake
Avoid complaints To get rid of the error "JRuby limited openssl loaded. gem install jruby-openssl for full support." do this:
jruby -S gem install jruby-openssl --no-rdoc --no-ri
Setup sqlite3 so that won't get "no such file to load – sqlite3" when trying to view "About your application's environment" * Edit (project dir)/db/database.yml and change "sqlite3" to "jdbcsqlite3" for each "adapter:" so that it looks like:
# SQLite version 3.x
#   gem install sqlite3-ruby (not necessary on OS X Leopard)
  adapter: jdbcsqlite3
  database: db/development.sqlite3
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
  adapter: jdbcsqlite3
  database: db/test.sqlite3
  timeout: 5000

  adapter: jdbcsqlite3
  database: db/production.sqlite3
  timeout: 5000
* Install gems
jruby -S gem install jdbc-sqlite3 --no-rdoc --no-ri
jruby -S gem install activerecord-jdbcsqlite3-adapter --no-rdoc --no-ri
IDea v7 * Download latest v1 of the Intellij IDea Ruby plugin from IDea v8 * See improvements * May be a good idea to splurge and get the latest for improvements to Ruby/JRuby development * Download latest v2 of the Intellij IDea Ruby plugin from For either version * Unzip it * Shutdown IDea * Move the unzipped directory into the /plugins/ directory (in OS X this is commonly /Applications/IntelliJ IDEA (version).app/plugins * Start IDea Create a new project and set the facet: * File -> New Project * Choose Create project from scratch * Click next * For project name type "jror hello world" * Click on Ruby Module * Click next * For Specify the Ruby SDK click "Configure" button to right * Click on yellow plus sign "+" to the left above the list of SDKs (you may only have 1.5 listed) * Choose JRuby SDK * Point it at the symlink you setup earlier * Click Ok * Be sure the JRuby SDK is selected * Click Next * Check Rails (if you try to check this before installing rails, it will say rails is not available. See steps above). If you click (More) you will see configuration for using an existing Rails app, directory for rails app files, preconfigure for selected database, and RSpec and/or Spec::Rails (RSpec on Rails) configuration * Click Next * Click Finish Congratulations! You have your first JRoR (JRuby on Rails) project setup! You can now right-click on the folders in the project view tree to the left to create a new Controller, Integration Test, Mailer, Migration, Model, Observer, Plugin, Resource, Scaffold, Session migration, HTML, XHTML, File, Directory, ActionScript Class, Builder(rxml) Template, Erb(rhtml) Template, Mxml Component, RSpec test Template, Ruby Script, TestUnit test Template, or Rjs Template. Make it say "Hello World"... * Right-click on jror hello world -> jror hello world -> app -> controllers and choose "New -> Controller". * Type "helloworld" into the controller name field and click ok. * Wait for a minute while it creates the new controller. * In the helloworld_controller.rb file, make it look like this:
class HelloworldController < ApplicationController
    def index
      @greeting = "hello world!"
* Right-click on jror hello world -> jror hello world -> app -> views -> helloworld and choose "New -> Erb(rhtml) Template". * Type "index.html" as the filename. * This will create a file called index.html.erb. * Edit that file and just add the following:
<%= @greeting %>
* Now goto Run -> Stop to stop the app if it is running. * Goto Run -> Run to run the application and wait for it to start up. * Visit http://localhost:3000/helloworld/ You can make changes to the controller and view without having to restart. Read the FAQ about how to create a new project: * Debugging * Continuous Integration * More info: * IntelliJ forum on Ruby/JRuby development: * * *

No comments: