Getting Started With JRuby on Rails for Sun GlassFish Enterprise Server v3 Prelude

Accessing a Database From a Rails Application

One of the main functions of Rails is to make a quick-and-easy task of creating an application that accesses a database. This section shows you the steps to create a simple application that accesses a book database using MySQLTM. It is assumed that you have already installed JRuby 1.1.4, Rails 2.1.1, and the required Gems.

ProcedureSetting Up the MySQL Database Server

  1. Download and install the MySQL 5.0 Community Server

  2. Configure the server according to the MySQL documentation, including entering a root password.

  3. Start the server.

ProcedureCreating a Database-Backed Rails Application

  1. Go to the <JRUBY_HOME>/samples directory of your JRuby 1.1.4 installation.

  2. Create the books application template so that it is configured to use the MySQL database:

    jruby -S rails books -d mysql
  3. Go to the books directory you just created.

  4. Open the config/database.yml file in a text editor.

  5. When prompted, enter your MySQL root password under the development heading in the database.yml file.

  6. Go back to the books directory if you are not already there.

  7. Create the database by running the following command:

    jruby -S rake db:create

    After the database creation is complete, you should see output similar to the following:

    ** Execute db:create

    The rake command invokes the Rake tool. The Rake tool builds applications by running Rake files, which are written in Ruby and provide instructions for building applications.

  8. Create the scaffold and the Book model for the application:

    jruby script/generate scaffold book title:string 
    author:string isbn:string description:text

    When you run the script/generate command you specify the name of the model, the names of the columns, and the types for the data contained in the columns.

    A scaffold is the set of code that Rails generates to handle database operations for a model object, which is Book in this case. The scaffold consists of a controller and some views that allow users to perform the basic operations on a database, such as viewing the data, adding new records, and editing records. Rails also creates the model object when generating the scaffold.

  9. Create the database tables:

    jruby -S rake db:migrate

    When Rails is finished creating the tables, you should see output similar to the following:

    CreateBooks: migrated (0.1322ms) =========

    If you need to reset the database later, you can run jruby —S rake db:reset.

ProcedureDeploying and Running the Database-Backed Web Application

With this task, you will deploy the books application to the GlassFish v3 Gem. You can alternatively deploy it to your regular Enterprise Server using directory-based deployment, as described in Deploying a Rails Application as a Directory.

  1. Go to <JRUBY_HOME>/samples/books.

  2. Deploy the application to the GlassFish v3 Gem by running the following command:

    jruby -S glassfish_rails books
  3. Run the application in your web browser using the following URL:

    http://localhost:3000/books

    The opening page says “Listing books” and has an empty table, meaning that there are no book records in the database yet. To add book records to the table, do the next step.

  4. Add records to the table by clicking the New book link on the index.html page.

  5. Enter the data for book on the new.html page and click Create.