The Source for Java Technology Collaboration
User: Password:



Arun Gupta

Arun Gupta's Blog

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

Posted by arungupta on September 07, 2007 at 05:35 AM | Comments (4)

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


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first)

  • I am getting a "HTTP Status 404 The requested resource () is not available." when I go to http://localhost:8080/RailsApplication9/say/hello. I don't see any errors in the log. Any suggestions?

    Posted by: jptarqu on December 05, 2007 at 06:12 AM

  • Hi,
    Same problem here. With a basic project, I have a 404 error (Mac os X, NetBeans 6.0 final)
    if I request the http://localhost:8080/RailsApplication9 url, the resulting page displays the content of the directory.

    Strange, because when I was using previous release candidates, it was working fine.

    Olivier

    Posted by: opoppon on December 06, 2007 at 02:13 AM

  • I've been able to reproduce the error with NB 6.0 and GlassFish v2 and following it up. Will let you know once I find out more details.

    -Arun

    Posted by: arungupta on December 11, 2007 at 06:14 PM

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

    Posted by: arungupta on December 12, 2007 at 09:59 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds