Exit Print View

Sun GlassFish Enterprise Server v3 Scripting Framework Guide

  This Document Entire Library
Print View

Document Information

Preface

1.  Using JRuby on Rails With Sun GlassFish Enterprise Server

Introduction to JRuby and Rails on Sun GlassFish Enterprise Server

What Is Ruby on Rails ?

What Is JRuby ?

JRuby on Rails, the Sun GlassFish Enterprise Server v3, and the GlassFish v3 Gem

Installation and Configuration of JRuby

To Install JRuby and Rails from Update Center

To Install JRuby as Standalone

To Install Rails Gem on JRuby

Enterprise Server v3 JRuby Container Configuration

Configuring JRuby Container Through Asadmin CLI

Configuring JRuby Runtime Pool

Configuring JRuby Container Through Administration Console

To Configure JRuby Container from Administration Console

Creating a Simple Rails Application

To Create the hello Application

To Create the Controller and the View

To Pass Data From the Controller to the View

To Use Rails Without a Database

Deploying and Running a Rails Application

To Deploy the Rails Application as a Directory

Accessing a Database From a Rails Application

To Set Up the MySQL Database Server

To Create a Database-Backed Rails Application

Accessing Java Libraries From a Rails Application

To Create the Rails Application That Accesses Java Libraries

To Create the Views That Display the Images Generated by Java2D Code

To Add Java2D Code to a Rails Controller

To Run a Rails Application That Uses Java 2D Code

Monitor Rails Applications on Enterprise Server v3

Monitoring for JRuby Container

Viewing JRuby Container Statistics

GlassFish v3 Gem

To Install the GlassFish v3 Gem

To Run a Rails Application on GlassFish v3 Gem

To Deploy and Run the Database-Backed Web Application

Using GlassFish v3 Gem CLI

Introduction to Warbler

What Is Warbler ?

Creating and Deploying a Simple Rails Application with Warbler

To Create a Rails Application

To Deploy the WAR File

Further Information

2.  Developing Grails Applications

3.  Jython on Django

4.  Scala and Lift

5.  PHP

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 MySQL. You should have already installed JRuby, Rails, and the required Gems.

To Set Up the MySQL Database Server

  1. Download and install MySQL database server:

    MySQL 5.0 Community Server.

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

  3. Start the server.

To Create a Database-Backed Rails Application

  1. Create or select a directory for creating a database-backed Rails application.

  2. Change to that directory.

  3. Create and configure a application template to use the MySQL database:

    jruby -S rails books -d mysql
  4. Change to the books directory that you just created.

  5. Replace mysql with jdbcmysql.

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

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

  8. Change back to the books directory if you are not already there.

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

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

  11. 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 the following command:

    jruby —S rake db:reset