Oracle GlassFish Server 3.0.1 Scripting Framework Guide

Creating a Simple Rails Application

After installing and configuring JRuby on GlassFish Server, you are ready to start coding. This section explains how to create a simple Rails application, named hello, that displays the following message:


Welcome to JRuby on Rails on the Oracle GlassFish Server!

The process of creating and deploying the hello application can be broken down into four subtasks:

After completing the procedures in this section, continue to Deploying and Running a Rails Application for information about deploying applications to GlassFish Server.

ProcedureTo Create the hello Application

This procedure explains how to create a simple Rails application, named hello, and deploy it on GlassFish Server 3.0.1.

Before You Begin

JRuby and Rails must be installed as described in Installing JRuby and Rails before proceeding with the instructions in this section.

  1. Select or create a directory in which you want to create the sample application.

    In this example, a directory named /apps/jruby-apps is used.

  2. Change to the directory you are using for your applications, and then create a Rails application named hello.


    cd /apps/jruby-apps
    jruby -S rails hello
    

    This command creates the hello subdirectory, which contains a set of automatically generated files and subdirectories.

    The directories containing the files that you will likely use most are:

    • app: Application code (controllers, helpers, models, views, layouts)

    • config: Configuration files (environments, initializers, locales), including database, boot, and route files

    • public: Files and resources that need to be accessed directly rather than accessed through the Rails call stack; includes images, style sheets, and HTML files

  3. Proceed with the instructions in To Create the Controller and the View.

ProcedureTo Create the Controller and the View

The next step in creating and deploying the hello application is to create an application controller and a default application view.

  1. Change to the directory in which you created the hello application in To Create the hello Application.

    In this example:


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

  3. Proceed with the instructions in To Pass Data From the Controller to the View.

ProcedureTo Pass Data From the Controller to the View

After creating a controller and a view for the hello application, the next step is to pass data from the controller to the view. This procedure explains how to set an instance variable in the controller and then access its value from the view.

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

  2. Add an instance variable named @hello_message to the action named index and then save the file.

    The file should now contain the following:


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

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

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

  4. Add the following output block at the end of the file and then save the file:


    <p><%= @hello_message %></p>

    The file should now contain the following:


    <h1>Home#index</h1>
    <p>Find me in app/views/home/index.html.erb</p>
    <p><%= @hello_message %></p>

    This JRuby code, embedded into the view, inserts the value of @hello_message into the page. When you deploy run the hello application, “Welcome to JRuby on Rails on the GlassFish Server" is displayed in your browser.

  5. At this point, you can either:

ProcedureTo Use Rails Without a Database

Although Rails is intended for creating database-backed web applications, this example is simple enough that it does not require one. In this case, you can 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 commented property in the file:


    #config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
  3. Uncomment the property by removing the pound (#) character from 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.

  5. Proceed to Deploying and Running a Rails Application for instructions on deploying the application to GlassFish Server.