Ruby on Rails is the free web application framework, written in the Ruby language, designed to make web development faster, simpler and more efficient. JRuby is a Java implementation of the Ruby interpreter.
With recent improvements to the JRuby interpreter, running Rails on JRuby is not only possible, but in some cases a preferred way to develop and deploy Rails applications. Some IT organizations are reluctant to install Ruby on Rails applications, but with JRuby and Warbler, RoR applications can be bundled up into Java WAR files and deployed on any modern Java application server, including Tomcat. Most enterprise-y IT organizations can deal with that.
Prerequisites
- JDK 1.5 (also known as JDK 5) or later installed on your machine. Make you’ve got the development kit and not just the JRE; to check, make sure you can run javac from the command line. ( Download the JDK from Sun )
- Tomcat 6 or later installed ( download it at tomcat.apache.org )
Installing JRuby and Rails
- Download the latest version of JRuby (at the time of this writing the current version is jruby-bin-1.1.2.zip)
- Extract the downloaded zip or tar.gz to a location on your machine where you want to install JRuby, for this document sake, we’ll call the new extracted folder JRUBY_HOME
- Add JRUBY_HOME/bin to the PATH environment variable
- Make sure the files in the bin directory are executable by the user you will be logged in as, especially if you are using a UNIX-like operating system such as Linux or Mac OS X.
- Open a terminal and type jruby -v. The Ruby version number will be displayed and you should see something like below …
> jruby -v ruby 1.8.6 (2008-05-28 rev 6586) [i386-jruby1.1.2]
Now …
- make sure that you have Rails 2.0 or later installed ( run gem install rails –include-dependencies ).
>gem install rails ... Installing RDoc documentation for actionmailer-2.1.0... Installing RDoc documentation for activeresource-2.1.0...
Now, we’re ready to get to work!
Build a Rails App
Open a command window and build a new Rails app as you normally would by running rails project-name. In my example I am going to use MySQL as the database to keep everything the same as my previous example.
>rails -d mysql wickedproject
create
create app/controllers
create app/helpers
...
create log/development.log
create log/test.log
Let’s make the same trivial example that I made in the Ruby on Rails Quick Start using the scaffolding to build the initial app and migrations to manage our schema. Make sure that you are in the root of your new rails app ( cd wickedproject )
Now let’s use the scaffolding to create our simple admin app. As of Rails 2.0, you can describe your model in the scaffold command and the migration file is created automatically for you.
>ruby script/generate scaffold Widget name:string description:string date_available:datetime size:float
exists app/models/
exists app/controllers/
...
create test/fixtures/widgets.yml
exists db/migrate
create db/migrate/20080701014138_create_widgets.rb
>
Now let’s run the migration to create our initial tables:
>rake db:migrate == 20080701012343 CreateWidgetTable: migrating ==================== -- create_table(:widgets) -> 0.0080s == 20080701012343 CreateWidgetTable: migrated (0.0080s) =========== >
Now, start this mother up:
>ruby script/server => Booting Mongrel (use 'script/server webrick' to force WEBrick) => Rails 2.1.0 application starting on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server ** Starting Mongrel listening at 0.0.0.0:3000 ** Starting Rails with development environment... ** Rails loaded. ** Loading any Rails specific GemPlugins ** Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart). ** Rails signals registered. HUP => reload (without restart). It might not work well. ** Mongrel 1.1.5 available at 0.0.0.0:3000 ** Use CTRL-C to stop.
Now check it out at http://localhost:3000/widgets/
Cool. You should have a simple CRUD app for our widgets, but that is not much more interesting than my previous quick start. Let’s bundle this thing up for deployment on Tomcat.
Creating a WAR File from Your Rails App
- Install Warbler (Warbler is a gem to make a .war file out of a Rails project)
>jruby -S gem install -y rails warbler ... Successfully installed rails-2.1.0 Successfully installed warbler-0.9.9 2 gems installed Installing ri documentation for warbler-0.9.9... Installing RDoc documentation for warbler-0.9.9...
Okay, now we can generate our war, just run jruby -S warble war:
>jruby -S warble war
jar cf wickedproject.war -C tmp/war .
>
Now there should be wickedproject.war in the root of your Rails app directory.
Now, by default, warble configures the database for production, so make sure to run db:migrate on your production database, by default named wickedproject_production
First set the RAILS_ENV environment variable to “production”.
Windows users:
>set RAILS_ENV=production >
Unix, Linux, Mac users:
>export RAILS_ENV=production >
Now run the db:migrate command again:
>rake db:migrate == 20080701012343 CreateWidgetTable: migrating ==================== -- create_table(:widgets) -> 0.0080s == 20080701012343 CreateWidgetTable: migrated (0.0080s) =========== >
Alright, almost done!
Copy the wickedproject.war to the Tomcat “/webapps/” directory and start up Tomcat. You should be done!
Try it out at:
http://localhost:8080/wickedproject/widgets/
Now hand that WAR to your IT department and say “deploy this thing! You can even put it on WebLogic or Websphere or JBoss or Glassfish, whatever you like!”