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!

28 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

  17. Riccardo Says:

    Thanks mate. I am evaluating JRuby and this is a clean, simple and very useful tutorial. Well it took me more than 120 secs, probably 240 :-)

  18. William Says:

    Good tutorial. What release of rails supports the jruby. I am running rails 1.2.3 and after creating the database I am doing a jruby -s rake db:migrate and am getting
    rake aborted!
    database configuration specifies nonexistent jdbcmysql adapter.

    Here is my database.yml

    # MySQL (default setup). Versions 4.1 and 5.0 are recommended.
    #
    # Install the MySQL driver:
    # gem install mysql
    # On MacOS X:
    # gem install mysql — –include=/usr/local/lib
    # On Windows:
    # gem install mysql
    # Choose the win32 build.
    # Install MySQL and put its /bin directory on your path.
    #
    # And be sure to use new-style password hashing:
    # http://dev.mysql.com/doc/refman/5.0/en/old-client.html
    development:
    adapter: jdbcmysql
    encoding: utf8
    database: testclient_development
    username: root
    password:
    port: 3306
    host: localhost

    # Warning: The database defined as ‘test’ will be erased and
    # re-generated from your development database when you run ‘rake’.
    # Do not set this db to the same as development or production.
    test:
    adapter: jdbcmysql
    encoding: utf8
    database: testclient_test
    username: root
    password:
    port: 3306
    host: localhost

    production:
    adapter: jdbcmysql
    encoding: utf8
    database: testclient
    username: root
    password:
    port: 3306
    host: localhost

    I have activerecord-jdbc-adapter-0.9.1 in the
    /org.jruby.1.2.6/lib/ruby/gems/1.8.gems directory?

    Any ideas???

  19. Huandel Says:

    Renan,

    Já viu este framework??? é o JRuby on Rails, deve ser bom….

  20. chetan Says:

    JRUBY am able to run test app .
    running backgroundrb is much pain. BGRB not working in JRUBY??? .
    Any other option ??!@#$

    Will JRUBY works better than RUBY-1.9 ?.

  21. endofhope's me2DAY Says:

    eoh의 생각…

    jruby getting started 따라하다 삽질 좀 했다. 일단 database.yml 설정을 jdbcmysql 으로 바꿔야 하는 부분이 빠져있고, 시냅틱으로 설치한 mysql의 charset 설정을 바꿔주는부분을 찾느라 대굴대굴. 정답은 …

  22. Haluk Durmus Says:

    I’ve no error or warning when I run jruby -S rake db:create:all.
    But when I want to run jruby -S rake db:migrat

    (in /data/home/haluk/dev/myapp)
    rake aborted!
    The driver encountered an error: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown database ‘myapp_development’

    (See full trace by running task with –trace)

    jruby -S rake db:create:all does not create db but why ?

    my db config:
    development:
    adapter: jdbcmysql
    encoding: utf8
    database: myapp_development
    username: root
    password: xxxx

    I can enter mysql server using username: root an passwort: xxxx.
    (mysql -u root -p)

    activerecord-jdbcmysql-adapte is installed aswell
    jdbc-mysql-5.0.4 activerecord-jdbc-adapter-0.9.2

    I’m using jruby-1.4.0RC1 and rails version 2.2

  23. Haluk Durmus Says:

    rails verion is 2.3.4 (not 2.2)

  24. Vladimir Sizikov Says:

    Haluk, please take a look at the following entry by Nick with explanation:

    http://blog.nicksieger.com/articles/2009/10/12/fresh-0-9-2-activerecord-jdbc-adapter-release

    Еssentially, with this new version of activerecord-jdbc, you don’t need to modify the database.yml anymore, but you need to run one additional command: jruby script/generate jdbc

  25. hamonika Says:

    Thanks everybody!!

    ————-
    >mysql -u root -p
    Enter password: *****
    Welcome to the MySQL monitor.
    Your MySQL connection id is 173
    Server version: 5.0.77-community-nt MySQL Community Edition (GPL)

    Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

    mysql>

    ————-
    >jruby -v
    jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) C
    lient VM 1.5.0_10) [x86-java]

    ————-
    >jruby -S gem list

    *** LOCAL GEMS ***

    actionmailer (2.3.4)
    actionpack (2.3.4)
    activerecord (2.3.4)
    activerecord-jdbc-adapter (0.9.2)
    activerecord-jdbcmysql-adapter (0.9.2)
    activeresource (2.3.4)
    activesupport (2.3.4)
    gem_plugin (0.2.3)
    jdbc-mysql (5.0.4)
    mongrel (1.1.5)
    rack (1.0.1)
    rails (2.3.4)
    rake (0.8.7)
    rspec (1.2.9)
    sources (0.0.1)
    ——————–

    >jruby -S gem install mongrel activerecord-jdbcmysql-adapter rails

    >jruby -S rails jmyapp -d mysql

    >cd jmyapp

    >jruby script/generate jdbc (Good!!)

    >no edit config/environment.rb

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

  26. BlueWhale Says:

    to Haluk Durmus:
    execute “jruby script/generate jdbc” in your app root:)

  27. Michael Says:

    Hi,

    since everybody seems to get it up and running, I must miss something important here … I followed the steps in the tutorial, but after entering the first post, I get the following error from the Action Controller:

    post_url failed to generate from {:controller=>”posts”, :action=>”show”, :id=>#<Post id: #, title: “MyFirstPost”, body: “This is my first post”, created_at: “2009-11-12 11:04:04″, updated_at: “2009-11-12 11:04:04″>}, expected: {:controller=>”posts”, :action=>”show”}, diff: {:id=>#<Post id: #, title: “MyFirstPost”, body: “This is my first post”, created_at: “2009-11-12 11:04:04″, updated_at: “2009-11-12 11:04:04″>}

    Extracted source (around line #13):

    10:
    11:
    12:
    13:
    14:
    15: ‘Are you sure?’, :method => :delete %>
    16:

    Can anybody help me getting started?

    Thanks,

    Michael

  28. Seb Says:

    Hi Michael,

    I am getting the same error than you.
    Did you find a solution ?

    Seb

Leave a Reply