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.
This procedure explains how to create a simple Rails application, named hello, and deploy it on GlassFish Server 3.0.1.
JRuby and Rails must be installed as described in Installing JRuby and Rails before proceeding with the instructions in this section.
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.
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
Proceed with the instructions in To 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.
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.
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 |
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.
Proceed with the instructions in To 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.
Open the hello/app/controllers/home_controller.rb file in a text editor.
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.
Open the hello/app/views/home/index.html.erb file in a text editor.
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.
At this point, you can either:
Proceed with the instructions in To Use Rails Without a Database, which are relevant to the hello sample, but will not be relevant for most applications used in a production environment.
Proceed directly to Deploying and Running a Rails Application for instructions on deploying the application to GlassFish Server.
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.
Open hello/config/environment.rb file in a text editor.
Look for the following commented property in the file:
#config.frameworks -= [ :active_record, :active_resource, :action_mailer ] |
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 ] |
Exit and save the file.
Proceed to Deploying and Running a Rails Application for instructions on deploying the application to GlassFish Server.