Joshua Java

JRuby on Rails

Posted on: May 30, 2008

I’ve been wanting to write tutorial on JRuby on Rails for my personal reference but couldn’t really find the time for it. Now here’s a short tutorial on JRuby on Rails that hopefully be useful for you.

Download the required prerequisites:
JDK: http://java.sun.com
JRuby: http://dist.codehaus.org/jruby/

Set proxy if you are behind a proxy by setting to environment variable http_proxy.

Set up your environment variables so you can access the jruby command straight from the console. First set the JRUBY_HOME enviroment variable, then add another entry for PATH variable: $JRUBY_HOME/bin.

Update the gems required for the rails project:
jruby -S jgem install rails mongrel rake ActiveRecord-JDBC activerecord-jdbc-adapter

Put in the jdbc driver inside: $JRUBY_HOME/lib
For this tutorial I use mysql-jdbc driver.

Create rails project by executing this command from the console:
jruby -S rails jugreg

Start the webserver by executing this command from the command line:
mongrel_rails start -e development

Now you can view your generated project from the browser by pointing to this URL:
http://localhost:3000

If you can see this screenshot from your browser then you’ve succeed creating Rails project with JRuby. As you can see, the environment is using JRuby implementation and JDBC for the database adapter.

Go inside to the rails project folder namely jugreg and open config/database.yml from there, edit it as such according to your database environment:
development:
adapter: jdbc
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/jugreg
username: joshua
password: password

Now you create the jugreg database. With mysql this is what I would do from mysql command line:
create database jugreg;

Now we’re going to generate all the source for Member with this command from the project folder:
jruby script/generate scaffold Member name:string position:string incumbency:string company:string website:string phone:string email:string javaprog:boolean usejava:boolean experience:integer

Then execute this command so rake will generate the member table on the database
jruby -S rake db:create:all
jruby -S rake db:migrate

Start the webserver by executing this command from the command line:
mongrel_rails start -e production
The “-e” flag indicates the environment you want to use. By default there are 3 enviroment as you can see from the config/environments

Now you can view member list page from the browser by pointing to this URL:
http://localhost:3000/members

If you want to see the entry form page from the browser, you can point on your browser to this URL:
http://localhost:3000/members/new

That’s all for now. Hopefully it will be useful for you. If you find any difficulties please don’t hesitate to contact me.

Tags: ,

5 Responses to "JRuby on Rails"

Maksudnya apa ya pak??
saya ini mau belajar java, tapi ga tahu mau mulai dari mana.

Mohon masukannya.

You don’t need to build rubygems. There’s already a ‘jgem’ command.

Good post!

As was noted, there is no need to install rubygems, since they come pre-built (and a little be adjusted for JRuby).

Also, be careful with direct invocation of mongrel_rails. If you have C-Ruby installed with mongrel, chances are that you’d start wrong mongrel. The safest (and recommended) way to make sure you start with JRuby is to use jruby -S command-name.

Also, since rails by default start with Mongrel if Mongrel is installed, you could just do: jruby script/server .

I wonder, what’s the difference between the regular Ruby on Rails and JRuby?

Jruby is running on top of JVM , adhilaras…

just like Jhyton, its a phyton on JVM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: