Miles to go …

September 7, 2007

JRuby on Rails, NetBeans 6 and GlassFish V2 – Simplified Steps

Filed under: netbeans, web2.0 — arungupta @ 12:02 am

The NetBeans IDE has simplified the steps to deploy JRuby on Rails application on GlassFish. This blog explains the steps and is an update to screencast #web6.

  1. Download the install the latest NetBeans 6 Nightly. I downloaded the Ruby pack by clicking on the "Download" button in the Ruby column.
  2. Create a new Rails Application
    1. Right-click in the Project window and select "New Project...". Take all the defaults as shown below:

    2. Click on "Next" and enter the values as shown below:

      Notice, we have selected "Add rake targets to support app server deployment (.war)". This downloads the goldfish (nee rails integration) plugin and installs in your application. Choose the database that you’d like to work with using the combo box:

      and then click on "Finish" button. I’m choosing the default database (MySQL) in this case.

  3. Create Database
    1. Download and install MySQL Community Server 5.0 (lets say MYSQL_HOME).
    2. Start MySQL database server by giving the command ‘mysqld -nt --user root‘ in MYSQL_HOME/bin directory on Windows or ‘./bin/mysqld_safe‘ from MYSQL_HOME directory on Unix flavors.
    3. Create a database by giving the following commands:

      mysqladmin -u root create RailsApplication9_development

  4. Create Generators
    1. Generate a model – Right-select the project, select "Generate..." and enter the values as shown below:

      and click on "OK".

    2. Generate a controller – Right-select the project, select "Generate..." and enter the values as shown:

      and click on "OK".

  5. Configure Model and Controller
    1. Configure Model
      1. In the NetBeans IDE, expand "Database Configurations", "migrate" and open "001_create_greetings.rb". Change the "self.up" helper method such that it looks like:

        def self.up
          create_table :greetings do |t|
            t.column :data, :string
          end
        end

      2. Right-select the project, select ‘Run Rake Target‘, ‘db‘, ‘migrate‘. This generates the appropriate database tables and the following is shown in the output window:

        (in C:/Users/Arun Gupta/Documents/NetBeansProjects/RailsApplication9)
        == CreateGreetings: migrating =================================================
        -- create_table(:greetings)
        -> 0.1010s
        == CreateGreetings: migrated (0.1110s) ========================================

      3. Add data to the database tables using the following commands in a shell window:

        C:\Program Files\MySQL\MySQL Server 5.0\bin>mysql --user root
        Welcome to the MySQL monitor. Commands end with ; or \g.
        Your MySQL connection id is 5
        Server version: 5.0.45-community-nt MySQL Community Edition (GPL)

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

        mysql> use RailsApplication9_development;
        Database changed
        mysql> insert into greetings values (1, "Hello from database!");
        Query OK, 1 row affected (0.10 sec)

        mysql> exit;
        Bye

         

      4. In the NetBeans IDE, expand "Configuration" and open "database.yml". Change the database entry in production environment to point to "RailsApplication9_development" instead of "RailsApplication9_production".
    2. Configure Controller
      1. Expand "Controllers", open "say_controller.rb" and change "hello" helper method such that it looks like:

        def hello
          @hello_string = Greeting.find(1).data;
        end

      2. Expand "Views", "say", open "hello.rhtml" and add the following fragment at the bottom of the page:

        <%= @hello_string %>

  6. Create a WAR file
    1. In the NetBeans IDE, right-select the project, select ‘Run Rake Target‘, ‘war‘, ‘standalone‘, ‘create‘ as shown here:

      and the following is shown in the output window:

      (in C:/Users/Arun Gupta/Documents/NetBeansProjects/RailsApplication9)
      Assembling web application
        Adding Java library commons-pool-1.3
        Adding Java library activation-1.1
        Adding Java library jruby-complete-1.0
        Adding Java library bcprov-jdk14-124
        Adding Java library rails-integration-1.1.1
        Adding web application
        Adding Ruby gem rails version 1.2.3
        Adding Ruby gem rake version 0.7.3
        Adding Ruby gem activesupport version 1.4.2
        Adding Ruby gem activerecord version 1.15.3
        Adding Ruby gem actionpack version 1.13.3
        Adding Ruby gem actionmailer version 1.3.3
        Adding Ruby gem actionwebservice version 1.2.3
        Adding Ruby gem ActiveRecord-JDBC version 0.5
      Creating web archive

    2. This creates "RailsApplication9.war" in the project directory.
  7. Download, Install and Start GlassFish.
    1. Download GlassFish recent build.
    2. Install by giving the command:

      java -jar filename.jar

      This creates "glassfish" directory in your current directory.

    3. In GlassFish install directory, set up the server by giving the following command:

      lib\ant\bin\ant -f setup.xml

    4. Start the server by giving the following command

      bin\asadmin start-domain

  8. Copy the WAR file (RailsApplication9.war) in "domains/domain/autodeploy" directory.

The application is accessible at "http://localhost:8080/RailsApplication9/say/hello".

Here are the improvements from the
last time:

  1. During the project creation, there was an option to select the database to be used by the application. This creates the "database.yml" using default values for that database.
  2. During the project creation, there was an option to add add rake targets for the WAR creation. This made the steps to explicitly install the repository and install the plugin redundant.
  3. ActiveRecord-JDBC 0.5 provides a simplified database configuration and is used by the "war:standalone:create" rake target. This allows the database to be accessed using the Ruby adapter instead of the JDBC adapter and also uses the original format of "database.yml".
     

Technorati: jruby ruby rubyonrails glassfish netbeans jrubyonglassfish mysql

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. JRuby 1.0.3 + Rails 2.0 Hello World – Additional Steps Required
  2. NetBeans 6.5 M1: GlassFish v3 + Rails
  3. jMaki on Rails – Reloaded for NetBeans 6.1 beta & Rails 2.0
  4. TOTD #3: Using JavaDB with JRuby on Rails
  5. TOTD #24: Getting Started with Rails 2.0.x in JRuby 1.0.3 and JRuby 1.1RC1

23 Comments »

  1. [Trackback] Earlier in a three-part series (part1, part2, part3) I showed how a JRuby application can be deployed on GlassFish. This screencast consolidates all the entries together and shows how such an application can be developed and deployed using NetBeans…

    Comment by Arun Gupta's Blog — September 7, 2007 @ 5:40 am

  2. when I’m trying to create the war file (‘Run Rake Target’, ‘war’, ’standalone’, ‘create’), Rake is crashing with a timeout.

    Comment by JS — September 7, 2007 @ 10:21 am

  3. Arun, what makes this app a *JRuby* on Rails application instead of a *Ruby* on Rails application–how do you specify it within the IDE? Also, once you choose, can you switch back and forth between JRuby / Ruby (perhaps to test performance, etc.)? For the latter question, I don’t think you can, because the code scripting is different, correct? Thanks, Glen

    Comment by Glen — September 7, 2007 @ 10:43 am

  4. [Trackback] The NetBeans 6 IDE comes pre-configured with JRuby interpreter. This TOTD explains how the JRuby interpreter can be swapped with a C-based Ruby interpreter and vice versa. Verify the JRuby interpreter Create a Rails Hello World using NetBeans 6 IDE….

    Comment by Arun Gupta's Blog — September 7, 2007 @ 2:43 pm

  5. I am trying to run through this howto on my Ubuntu system, but the migrate step (5.1.2) bombs. Here is the trace:
    ** Invoke db:migrate (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    ** Execute db:migrate
    rake aborted!
    can’t convert nil into String
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/vendor/mysql.rb:111:in `real_connect’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:389:in `connect’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:152:in `initialize’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:82:in `new’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:82:in `mysql_connection’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:262:in `send’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:262:in `connection=’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/query_cache.rb:54:in `connection=’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:230:in `retrieve_connection’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:78:in `connection’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/migration.rb:284:in `migrate’
    /home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/databases.rake:4

    Comment by Keith — September 10, 2007 @ 9:23 pm

  6. Keith, It seems like this is an issue with ActiveRecord-JDBC. I filed an issue at: http://jira.codehaus.org/browse/JRUBY-1341.

    Comment by Arun Gupta — September 11, 2007 @ 11:10 am

  7. Ruby is new for me.

    Comment by heart5 — September 12, 2007 @ 1:32 am

  8. The best place to get started with Ruby is http://www.ruby-lang.org/en/documentation/. Or if you want to learn JRuby then you can start at: http://jruby.codehaus.org/.

    Comment by Arun Gupta — September 12, 2007 @ 6:27 am

  9. [Trackback] Using the instructions followed in JRuby Hack Day and taking some help from Nick, I figured out how to use the JDBC connection pools configured in GlassFish using the JNDI names. All the commands given below are relevant for GlassFish…

    Comment by Arun Gupta's Blog — September 12, 2007 @ 6:51 am

  10. [Trackback] UPDATE: Simplified steps for GlassFish V2 are available here and for V3 here. Ashish described how to create RoRaWAR (Ruby on Rails as Web ARchive). I decided to try these instructions on GlassFish V2 b46. Here are the steps I…

    Comment by Arun Gupta's Blog — September 13, 2007 @ 8:55 pm

  11. [Trackback] UPDATE: Simplified steps for GlassFish V2 are available here and for V3 here. Follow up from here. In this post I show how a Rails app can talk to database. Here are the steps I followed: Deploy a RoR application…

    Comment by Arun Gupta's Blog — September 13, 2007 @ 8:55 pm

  12. [Trackback] UPDATE: Simplified steps for GlassFish V2 are available here and for V3 here. Follow up from here. In this post I’ll show how a Ruby-on-Rails (RoR) application, talking to MySQL database, can be deployed as a WAR file on GlassFish…

    Comment by Arun Gupta's Blog — September 13, 2007 @ 8:56 pm

  13. [Trackback] Jerome has been working on GlassFish gem for Rails. Read the interesting discussion on dev@glassfish. This blog announces a technology preview of this gem and describes the steps to try it out. Download GlassFish Gem from here. If you already…

    Comment by Arun Gupta's Blog — September 14, 2007 @ 6:13 am

  14. Thanks for the instructions. This mostly worked for me on MacOSX with a couple of modifications.

    1) I also received Keith’s error when running migrations. Adding "host: localhost" to each of the database.yml connection settings (development, test, and production) worked around that. Never had to set that before however. I’d be curious to know the best way to use the JDBC connector as an alternate.

    2) The MacOS build for NetBeans includes a separate installer for Glassfish which installed glassfish at /Applications/NetBeans/glassfish-v2b58g/ so I was able to skip running "ant -f setup.xml". Also, "domains/domain/autodeploy" was "domains/domain1/autodeploy" for my installation.

    Comment by victor cosby — September 25, 2007 @ 10:05 pm

  15. Thanks for this. It is very clear. It worked – eventually. The issues (which others might be interested in) for me were two. (i) In database.yml change socket: /var/lib/mysql/mysql.sock to host: localhost (as noted above). (ii) I am behind a firewall and cut off from web based resources. I needed to download and copy the following into JRUBY_HOME/lib – commons-pool-1.3.jar, activation-1.1.jar, jruby-complete-1.0.jar, bcprov-jdk14-124.jar, rails-integration-1.1.1.jar

    Comment by Richard Forster — October 29, 2007 @ 10:37 pm

  16. The deployed application does not seem to handle ‘redirect_to’ instructions properly. For instance you could expand ‘hello’ to the following

    def hello
    @hello_string = Greeting.find(1).data;
    if request.post?
    redirect_to(:action => ‘tom’)
    end
    end

    If there is an appropriate view for ‘tom’ this will work fine in webrick. If the application is deployed (as described above) to Glassfish
    The initial invocation of say/hello is ok
    but the attempted redirection does not generate a complete URL:
    (localhost:8080/admin/tom)
    I have tried fiddling with the routes.rb but with no success. link_to seems to be ok.

    Comment by Richard Forster — November 11, 2007 @ 6:27 pm

  17. thanks all.

    Comment by evden eve nakliyat — November 25, 2007 @ 10:00 am

  18. Nice tutorial Arun. I had been struggling to get my database access to work until reading your blog. The key step for me was 5.4 – I didn’t think to update the production database information in database.yml. Thanks, Brian.

    Comment by Brian Leonard — November 26, 2007 @ 5:25 pm

  19. Cool, glad you found it useful.

    Comment by Arun Gupta — November 26, 2007 @ 7:00 pm

  20. rake war:standalone:create
    gives

    (in c:/ruby/app)
    info: Assembling web application
    info: Packing needed java lib….
    info: Packing needed Ruby gems…
    info: Packing needed files…
    info: Creating web archive
    reak aborted!
    Error: failed to create archive,error code

    i’m trying to deploy my rails application into tomcat, installed goldspike but rake is not working
    (using ruby not jruby)

    Comment by disha — December 14, 2007 @ 2:21 am

  21. If you are using NetBeans 6, then this is a known issue as described at:

    http://www.netbeans.org/issues/show_bug.cgi?id=123900

    I plan to try this outside NetBeans and report back.

    Comment by Arun Gupta — December 14, 2007 @ 7:15 am

  22. Not using any type of editors or IDE’s

    Comment by disha — December 15, 2007 @ 4:19 am

  23. i’m trying to deploy my rails application into tomcat, installed goldspike but rake is not working
    (using ruby not jruby)

    Comment by BATTERY — November 27, 2008 @ 4:53 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress