TOTD #24: Getting Started with Rails 2.0.x in JRuby 1.0.3 and JRuby 1.1RC1


This TOTD (Tip Of The Day) shows how to install Rails 2.0.x in JRuby 1.0.3 and JRuby 1.1 RC1. Then it describes how a simple Rails application can be created and deployed using WEBrick.

First, lets deal with installing Rails 2.0.x in JRuby 1.0.3 and JRuby 1.1 RC1.

Step 1: How to install Rails 2.0.x in JRuby 1.0.3 ?
  • Download and Unzip JRuby 1.0.3. This creates "jruby-1.0.3" directory.
  • Install Rails by giving the following command in the newly created directory:

    bin/jruby -S gem install rails --include-dependencies --no-ri --no-rdoc
    Bulk updating Gem source index for: http://gems.rubyforge.org
    Successfully installed rails-2.0.2
    Successfully installed activesupport-2.0.2
    Successfully installed activerecord-2.0.2
    Successfully installed actionpack-2.0.2
    Successfully installed actionmailer-2.0.2
    Successfully installed activeresource-2.0.2

Step 2: How to install Rails 2.0.x in JRuby 1.1RC1?

  • Download and Unzip JRuby 1.1 RC1. This creates "jruby-1.1RC1" directory.
  • Install Rails by giving the following command in the newly created directory:

    bin/jruby -S gem install rails --no-ri --no-rdoc
    Updating metadata for 632 gems from http://gems.rubyforge.org
    ................................................................................................................
    ................................................................................................................
    ................................................................................................................
    ................................................................................................................
    ................................................................................................................
    ........................................................................
    complete
    Successfully installed activesupport-2.0.2
    Successfully installed activerecord-2.0.2
    Successfully installed actionpack-2.0.2
    Successfully installed actionmailer-2.0.2
    Successfully installed activeresource-2.0.2
    Successfully installed rails-2.0.2
    6 gems installed

Step 3: How to create and deploy a trivial Rails app ?

Once Rails is installed in JRuby 1.x, then the steps to develop and deploy a simple application are exactly same and given below:
  1. Create the application
    1. Create a Rails template app by giving the following command:

      jruby -S rails --database mysql hello
    2. Create Controller and View by giving the following command:

      cd hello
      jruby script/generate controller say hello
    3. Edit Controller by giving the following command:

      vi app/controllers/say_controller.rb

      and updating the "hello" helper method to look like:

      def hello
          @hello_string = "Hello from Rail 2.0.2!!"
      end
    4. Edit View by giving the following command:

      vi app/views/say/hello.html.erb

      and adding the following line at the end:

      <%= @hello_string %>
  2. Rails 2.0 default is to configure a database for the application. Following Convention-over-Configuration, either the defaults can be taken in which case you need to configure the database. Or override the convention by specifying "config.frameworks -= [ :active_record, :action_mailer ]" in "config/environment.rb". Follow the steps below if you prefer configuring the database:
    1. Start MySQL server by giving the following command:

      sudo /usr/local/mysql/bin/mysqld_safe --user root
      Starting mysqld daemon with databases from /usr/local/mysql/data
    2. Create development database by giving the following command:

      jruby -S rake db:create
  3. Test the App
    1. Start WEBrick, using JRuby 1.0.3, by giving the following command:

      jruby script/server
      => Booting WEBrick...
      => Rails application started on http://0.0.0.0:3000
      => Ctrl-C to shutdown server; call with --help for options
      [2008-02-05 23:35:02] INFO  WEBrick 1.3.1
      [2008-02-05 23:35:02] INFO  ruby 1.8.5 (2007-12-15) [java]
      [2008-02-05 23:35:02] INFO  WEBrick::HTTPServer#start: pid=13446204 port=3000

      The application is now accessible at http://localhost:3000/say/hello. Accessing the application shows the following output in console:

      127.0.0.1 - - [05/Feb/2008:23:35:31 PST] "GET /say/hello HTTP/1.1" 200 89
      - -> /say/hello
      127.0.0.1 - - [05/Feb/2008:23:35:32 PST] "GET /say/hello HTTP/1.1" 200 89
      - -> /say/hello
      127.0.0.1 - - [05/Feb/2008:23:35:32 PST] "GET /say/hello HTTP/1.1" 200 89
      - -> /say/hello

    2. Start WEBrick using JRuby 1.1RC1, then the following output is shown:

      => Booting WEBrick...
      => Rails application started on http://0.0.0.0:3000
      => Ctrl-C to shutdown server; call with --help for options
      [2008-02-05 23:37:43] INFO  WEBrick 1.3.1
      [2008-02-05 23:37:43] INFO  ruby 1.8.6 (2008-01-07) [java]
      [2008-02-05 23:37:43] INFO  WEBrick::HTTPServer#start: pid=2533 port=3000

      Note, that even though the application was created using JRuby 1.0.3, it can be easily invoked using JRuby 1.1RC1 because they are using the same Rails version. Accessing the application in this case shows the following output:

      127.0.0.1 - - [05/Feb/2008:23:37:51 PST] "GET /say/hello HTTP/1.1" 200 89
      - -> /say/hello
So, you built a simple Rails 2.0.2 application and deployed using JRuby 1.0.3 and JRuby 1.1 RC1.

Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.


Technorati: totd rubyonrails jruby mysql webrick
Comments:

[Trackback] GlassFish v3 Gem allows JRuby-on-Rails application to be launched in GlassFish v3 server. It provides a robust alternative to WEBrick and Mongrel for development and deployment of your JRuby-on-Rails applications. The Gem was originally announced here...

Posted by Arun Gupta's Blog on February 07, 2008 at 08:33 PM PST #

[Trackback] JRuby 1.1 RC2 was released earlier today an GlassFish v3 gem works like a charm on it :) Here are the steps for quick verification ... Download JRuby 1.1 RC2. Install Rails and create a simple Rails application as described...

Posted by Arun Gupta's Blog on February 16, 2008 at 12:08 PM PST #

I am of the opinion that MySQL is by and large more difficult by far to setup and configure then anything else in the basic rails stack. This is particularly true trying to develop with a Windows desktop, a OSX laptop and an Ubuntu Gusty production environment. I therefore suggest that it would be sensible to either modify or augment this post with instructions about how to use a simple cross-platform file/memory database engine.
For MRI I have been using SQLite3 is there some equivalent in the world of java? If so how do I use it?

Posted by StarTrader on February 17, 2008 at 07:45 AM PST #

Thanks for the feedback! I've not used SQLite3 but will do so for a subsequent entry. However I've installed MySQL on Windows with a single button click and started without any problem. On MacOS 10.5 (Leopard), it started without a problem as well. All my previous MySQL related entries on this blog are available at http://blogs.sun.com/arungupta/tags/mysql. More details about MySQL installation are available at: http://blogs.sun.com/arungupta/entry/restful_representation_of_sakila_using

Posted by Arun Gupta on February 17, 2008 at 08:58 AM PST #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today