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!

16 Responses to “120 seconds guide to JRuby on Rails”

  1. khelll Says:

    is there a way to run mongrel cluster?

  2. Vladimir Sizikov Says:

    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

  3. Jay Says:

    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!

  4. Vladimir Sizikov Says:

    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.

  5. Leandro Says:

    Good!!!

    I have developed with the NetBeans 6.1 and liked so much.

  6. Pwhndvve Says:

    Honi soit legate left buy cytotec then announced daughters.

  7. Starting Rails on Mongrel with Jruby. « Das Mekio Magazin Says:

    [...] 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 [...]

  8. pratap Says:

    great ,

    it works man

  9. Robin Says:

    How can I migrate a working Ruby on Rails project to a JRuby on Rails project?

  10. JRuby/Rails « Bandwagon Says:

    [...] 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/ [...]

  11. Manmay Says:

    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.

  12. Franky Says:

    For me it worked better using sqlite3 instead of mysql

  13. Ilia Lobsanov Says:

    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.

  14. fuzzbuzz Says:

    Nice tutorial.

    But how do you call java code (e.g. your own jars or classes) from your controller?

  15. hamonika Says:

    >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!

  16. Jason Rogers Says:

    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

Leave a Reply