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

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 “Welcome to JRuby on Rails on the Sun GlassFish Enterprise Server!”

ProcedureCreating the hello Application

  1. Go to <JRUBY_HOME>/samples directory.

  2. Create a Rails application called hello:

    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.

ProcedureCreating the Controller and View

By doing this task, you can 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. Go to the <JRUBY_HOME>/samples/hello directory you created in the previous task.

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

    jruby script/generate controller home index

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

ProcedurePassing Data From the Controller to the View

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

  1. Open <JRUBY_HOME>/samples/hello/app/controllers/home_controller.rb in a text editor.

  2. 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"

    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, it makes the @hello_message variable available to index.html.erb.

  3. Save the file.

  4. Open <JRUBY_HOME>/samples/hello/app/views/home/index.html.erb file in a text editor.

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

  6. Save the file.

ProcedureUsing 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 need to edit the enviroment.rb configuration file to indicate that your application does not use a database.

  1. Open <JRUBY_HOME>/samples/hello/config/environment.rb file in a text editor.

  2. Remove the pound character (#) in front of line 21 to uncomment it so that it reads as:

    config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

    ActiveRecord supports database access for Rails applications. When you create model objects, you will most likely base them on ActiveRecord::Base.

  3. Save the file.