Miles to go ...

Arun Gupta is a Technology Evangelist for Web Services and Web 2.0 Apps at Sun. He was the spec lead for APIs in the Java platform, committer in multiple Open Source projects, participated in standard bodies and contributed to Java EE and SE releases.
« Learn to REST using... | Main | TOTD #7: Switch... »

http://blogs.sun.com/arungupta/date/20070907 Friday September 07, 2007

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

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

del.icio.us | furl | simpy | slashdot | technorati | digg
Comments:

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

Posted by Arun Gupta's Blog on September 07, 2007 at 05:40 AM PDT #

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

Posted by JS on September 07, 2007 at 10:21 AM PDT #

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

Posted by Glen on September 07, 2007 at 10:43 AM PDT #

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

Posted by Arun Gupta's Blog on September 07, 2007 at 02:43 PM PDT #

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

Posted by Keith on September 10, 2007 at 09:23 PM PDT #

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

Posted by Arun Gupta on September 11, 2007 at 11:10 AM PDT #

Ruby is new for me.

Posted by heart5 on September 12, 2007 at 01:32 AM PDT #

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

Posted by Arun Gupta on September 12, 2007 at 06:27 AM PDT #

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

Posted by Arun Gupta's Blog on September 12, 2007 at 06:51 AM PDT #

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

Posted by Arun Gupta's Blog on September 13, 2007 at 08:55 PM PDT #

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

Posted by Arun Gupta's Blog on September 13, 2007 at 08:55 PM PDT #

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

Posted by Arun Gupta's Blog on September 13, 2007 at 08:56 PM PDT #

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

Posted by Arun Gupta's Blog on September 14, 2007 at 06:13 AM PDT #

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.

Posted by victor cosby on September 25, 2007 at 10:05 PM PDT #

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

Posted by Richard Forster on October 29, 2007 at 10:37 PM PDT #

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.

Posted by Richard Forster on November 11, 2007 at 06:27 PM PST #

thanks all.

Posted by evden eve nakliyat on November 25, 2007 at 10:00 AM PST #

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.

Posted by Brian Leonard on November 26, 2007 at 05:25 PM PST #

Cool, glad you found it useful.

Posted by Arun Gupta on November 26, 2007 at 07:00 PM PST #

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)

Posted by disha on December 14, 2007 at 02:21 AM PST #

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.

Posted by Arun Gupta on December 14, 2007 at 07:15 AM PST #

Not using any type of editors or IDE's

Posted by disha on December 15, 2007 at 04:19 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
« Learn to REST using... | Main | TOTD #7: Switch... »

Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.

--> ajax ajaxworld conf eclipse fitness gem glassfish glassfishday hyderabad india indigo interoperability javaone javaone2008 jax-ws jmaki jpa jruby marathon metro microsoft mysql netbeans phobos photography presos railsconf ruby rubyonrails running runninglog runsfm screencast siliconvalleymarathon sun suntechdays swdp tango theserverside totd training traveltips v3 vista wcf web2.0 webservices windows wsaddressing wsit
Project Tango: Adding Quality of Service and .NET Interoperability to the Metro Web Services Stack
Locations of visitors to this page

calendar

« May 2008
SunMonTueWedThuFriSat
    
11
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
       
Today

Stats

Today's Page Hits: 4654


Total # blog entries: 598
Total # comments: 1642

www.flickr.com
This is a Flickr badge showing public photos from ArunGupta. Make your own badge here.
Add to Technorati Favorites

Last 50