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

Creating a Simple Rails Application

After completing your installations, you are ready to start coding. This section shows you how to create a simple application that displays the following message:

Welcome to JRuby on Rails on the Sun GlassFish Enterprise Server!

To Create the hello Application

  1. Select a directory to create a sample application. For example:

    /apps/jruby-apps

  2. Create a Rails application called hello in that directory:

    cd /apps/jruby-apps

    jruby -S rails hello

    This command creates the hello directory, which contains a set of automatically-generated files and directories.

    The directories containing the files that you'll use the most are:

    • app: Contains your application code.

    • config: Contains configuration files, such as database.yml, which you use to configure a database.

    • public: Contains files and resources that need to be accessed directly rather than accessed through the Rails call stack. These include images and straight HTML files.

To Create the Controller and the View

The following task describes how to create a controller and a default view for your application. The controller handles requests, dispatches them to other parts of the application as necessary, and determines which view to render. The view is the file that generates the output to the browser. In Rails, views are typically written with ErB, a templating mechanism.

  1. Change to the directory where you created the hello application in the previous task. For example:

    /apps/jruby-apps/hello

  2. Create a controller and default view for your application:

    jruby script/generate controller home index

    You should see a controller file called home_controller.rb in the hello/app/controllers directory and a view file called index.html.erb in the hello/app/views directory.

To Pass Data From the Controller to the View

Exchanging data between the controller and the views is a common task in web application development. This section describes how to set an instance variable in the controller and access its value from the view.

  1. Go to the controller that you created in the previous task.

  2. Open the hello/app/controllers/home_controller.rb file in a text editor.

  3. Add an instance variable called @hello_message to the action called index, so that the controller looks like this:

    class HomeController < ApplicationController
    def index
    @hello_message = "Welcome to JRuby on Rails on the Sun GlassFish Enterprise Server"
    end
    end

    In Rails, the actions are supposed to map to views. So, when you access the index.html.erb file, the index action executes. In this case, the index action makes the @hello_message variable available to index.html.erb.

  4. Save the file.

  5. Open the hello/app/views/home/index.html.erb file in a text editor.

  6. At the end of the file, add the following output block:

    <%= @hello_message %>

    This JRuby code, embedded into the view, inserts the value of @hello_message into the page. When you run the application, you can see “Welcome to JRuby on Rails on the GlassFish Enterprise Server” in your browser.

  7. Save the file.

To Use Rails Without a Database

Although Rails is intended for creating database-backed web applications, this example is simple enough that it doesn't require one. In this case, you must edit the enviroment.rb configuration file to indicate that your application does not use a database.

  1. Open hello/config/environment.rb file in a text editor.

  2. Look for the following pattern in the file:

    #config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
  3. Remove the pound (#) character in the beginning of the line. The line should now read as follows:

    config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
  4. Exit and save the file.