120 seconds guide to JRuby on Rails
So, you have that new and shiny JRuby 1.1 and would like to try it out with rails. Here’s a quickest guide to do so! The guide assumes that you want to use MySQL as the database, and it has already been installed.
First, install the following gems:
- mongrel - simple but powerful web server.
- activerecord-jdbcmysql-adapter - all you need for activerecord on JRuby to talk to MySQL.
- rails - well, the Ruby on Rails proper.
The command line:
1: jruby -S gem install mongrel activerecord-jdbcmysql-adapter rails
Create a sample rails application with MySQL backend:
1: jruby -S rails myapp -d mysql
Enter the newly-created “myapp” directory, then modify the config/database.yml. First and foremost, you need to adjust the adapter name, and instead of ‘mysql’ you should specify ‘jdbcmysql’. You might also want to delete the lines starting with “socket:”.
Here’s a simple example for the development environment:
1: development:
2: adapter: jdbcmysql
3: encoding: utf8
4: database: myapp_development
5: username: root
6: password:
Now, it’s time to create our database:
1: jruby -S rake db:create:all
The next step is to create some minimal scaffolding so that you could actually play with some dynamic functionality and database access:
1: jruby script/generate scaffold post title:string body:text published:boolean
We need to update the database after that:
1: jruby -S rake db:migrate
And we’re basically done here, just start rails via:
1: jruby script/server
and point your browser to the:
Enjoy!
April 13th, 2008 at 11:40 pm
is there a way to run mongrel cluster?
April 15th, 2008 at 9:55 am
Khell, yes there is. Take a look here: http://ola-bini.blogspot.com/2007/05/announcing-mongreljcluster.html
Also, there was a bug in early days of JRuby 1.1RC, but it was fixed for JRuby 1.1: http://jira.codehaus.org/browse/JRUBY-1705
April 22nd, 2008 at 8:53 pm
Hi,
Do you (or anyone) know the “official” list of things to install when working with Jruby, rails and mysql?
I’ve seen reference to needing to install mysql-connector into the jruby install directory, or activerecord-jdbc-adapter, or just the activerecord-jdbcmysql-adapter you mentioned above.
thanks!
April 22nd, 2008 at 9:12 pm
Jay, the list of gems, provided in the blog entry, is what’s needed, nothing else. Well, activerecord-jdbcmysql-adapter gem has dependencies on other gems and they will be installed automatically.
July 10th, 2008 at 11:03 pm
Good!!!
I have developed with the NetBeans 6.1 and liked so much.
August 9th, 2008 at 6:44 pm
Honi soit legate left buy cytotec then announced daughters.
October 13th, 2008 at 10:19 am
[...] Getting a database running on mongrel is really quite easy. A few failed attempts on starting “glassfish” on my machine made me try this link: 120 seconds [...]
November 27th, 2008 at 1:41 pm
great ,
it works man
December 9th, 2008 at 2:44 pm
How can I migrate a working Ruby on Rails project to a JRuby on Rails project?
January 13th, 2009 at 6:58 pm
[...] JRuby/Rails After several failed attempts at getting jruby/rails to work nicely with sqlite3, I fell back to mysql w/ this tutorial: http://blog.emptyway.com/2008/04/08/120-seconds-guide-to-jruby-on-rails/ [...]
January 15th, 2009 at 7:26 am
How can we implement Hibernate into a JRuby On Rails project.
Where to fit the servlet.xml,web.xml, pojos, hbm mapping files, etc into the rails directory structure.
February 17th, 2009 at 1:54 pm
For me it worked better using sqlite3 instead of mysql
April 28th, 2009 at 4:50 pm
if you get stuck at “jruby -S rake db:create:all” using mysql, create the myapp_production database manually and then run the db:create:all command.
June 8th, 2009 at 9:55 am
Nice tutorial.
But how do you call java code (e.g. your own jars or classes) from your controller?
June 12th, 2009 at 4:16 pm
>jruby -S gem install mongrel activerecord-jd
bcsqlite3-adapter rails
>jruby -S rails myapp -d sqlite3
>cd myapp
>edit config/environment.rb
development:
adapter: jdbcsqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
>jruby -S rake db:create:all
>jruby script/generate scaffold post title:st
ring body:text published:boolean
>jruby -S rake db:migrate
>jruby script/server
http://localhost:3000/posts
very good!!
thanks!
July 3rd, 2009 at 3:35 pm
fuzzbuzz: to call your own Java code you need to first make sure that the classes or JARs are in your path. Then it’s just a matter of requiring them in your controller (see http://kenai.com/projects/jruby/pages/CallingJavaFromJRuby).
require ‘path/to/mycode.jar’
class MyController < ApplicationController
def index
…
Java::MyJavaPackage::MyJavaClass.do_something
…
end
end