Writing Your First Ruby-on-Rails Web Application

By Rick Palkovic, February 2009  

Writing a Ruby-on-Rails web application is easy using the NetBeans IDE with its integrated JRuby interpreter and its support for the GlassFish application server and the MySQL relational database management system (RDBMS). This article shows you how to create a simple Ruby-on-Rails program in the OpenSolaris operating environment without typing a single line of code.


This tutorial assumes that you have installed the OpenSolaris 2008.11 operating system. As an alternative to a direct installation, you can install in another operating system using Virtual Box, as the author did when developing this tutorial. Installing the tools required for the tutorial is easy on OpenSolaris because they are integrated into the operating system.

The NetBeans IDE has been ported to other popular operating systems, and this tutorial works well on them too, with some modifications. For example, you will need to download and configure some of the necessary software individually. Specifically, you will need:

  • The NetBeans IDE with Ruby support and the JRuby interpreter
  • The GlassFish application server

Before getting started, read on to learn a bit more about these three key software tools. If you are impatient to start, you can skip directly to the first step in the tutorial, Installing Sun Web Stack.

About JRuby and the GlassFish Application Server

Although you can develop a Ruby-on-Rails application in a traditional Ruby environment, there are advantages to deploying your Ruby application in JRuby on the GlassFish application server.

  • The GlassFish application server is suitable for large-scale application deployments on a variety of platforms. When you develop in GlassFish, you do not need one set of software for developing your application and another set of software for deploying it.
  • You can deploy multiple Rails applications to a single GlassFish instance.
  • The GlassFish application server gives your Rails application the ability to handle multiple requests simultaneously.
  • With JRuby, you can access the rich set of Java libraries from your Rails application.
  • Your JRuby-on-Rails application can spin off multiple threads because JRuby uses Java threads, which map to native Ruby threads. Furthermore, you can pool these threads.
  • You can use the powerful and secure support of Java Unicode strings with your Rails application.
About the MySQL RDBMS

This tutorial uses the MySQL RDBMS. The community version is available for download at no cost.

  • In the OpenSolaris OS, the MySQL RDBMS is part of the system's integrated developer tools. You can install it with the Package Manager, as described below.
  • For other operating systems, you can download the MySQL RDBMS from the MySQL web site. For use with NetBeans 6.5, you will need MySQL version 5.x.
  • For the Solaris 10 operating system, the MySQL RDBMS is available as part of the Sun Web Stack download.
About the NetBeans IDE

The NetBeans IDE, version 6.5, is available for use with popular operating systems other than Solaris and OpenSolaris. Steps for completing the tutorial will be very similar regardless of the operating system you use.

For a tutorial on writing a Ruby application for JRuby and GlassFish without using the NetBeans IDE, see Getting Started With JRuby on Rails for the GlassFish v3 Application Server.

As with the MySQL RDBMS, there are several ways to obtain and install the NetBeans IDE.

  • In the OpenSolaris OS, the NetBeans IDE is part of the system's integrated developer tools. You can install it with the Package Manager, as described below.
  • Download and install the IDE directly from netbeans.org. To use this tutorial with an operating system other than OpenSolaris, download and install the NetBeans IDE for your platform.
  • If you install the Sun Web Stack for the Solaris or Linux OS, the NetBeans IDE is installed as part of the stack.
Installing Sun Web Stack

On the OpenSolaris operating system, you can get MySQL, JRuby, the NetBeans IDE, and other developer tools by installing the Sun Web Stack. You can install Sun Web Stack from the command line or through the OpenSolaris graphical user interface (GUI). Instructions for a command-line installation can be found in the article Installing Sun Web Stack on OpenSolaris OS. This tutorial uses the GUI procedure.

To install Sun Web Stack with the GUI:

  1. On the desktop or in the OpenSolaris menu bar, click the Add More Software icon. Alternatively, from the menu bar choose System > Administration > Package Manager. The Package Manager window opens.
    Figure 1. Package Manager Menu
  2. In the Package Manager window, click the checkbox next to amp-dev.
    Figure 2. Package Manager Window
  3. In the Package Manager window, click Install/Update and follow the prompts to install Sun Web Stack.
Initializing Sun Web Stack

You will now initialize Sun Web Stack. Among other actions, initialization places Web Stack items in the Applications > Developer Tools menu.

To initialize Sun Web Stack:

  • From the OpenSolaris menu bar, choose Applications > Developer Tools > Web Stack Initialize.
    Figure 3. Initializing Sun Web Stack
    After initialization, your menu will look like the following figure:
    Figure 4. Initialized Sun Web Stack Menu
    Click the image to enlarge it.

Note: If your Developer Tools menu doesn't have the items shown in the figure, restart the GUI by opening a Terminal window and typing the command pkill gnome-panel. The new menu items will then become available.

Starting the NetBeans IDE

Start the NetBeans IDE by choosing Applications > Developer Tools > Netbeans IDE from the menu bar.

Figure 5. Starting the NetBeans IDE
Installing the GlassFish Application Server

Click the Services tab on the left side of the NetBeans IDE window. There you see the services that are available for your projects. When you expand the Servers node, you see that only the default WEBrick server is available. To use GlassFish V3 Prelude as your application server, you need to add an instance in the NetBeans IDE.

To add a server instance:

  1. Right-click the Servers node and choose Add Server from the menu.
    Figure 6. Adding a Server Instance
    The Add Server Instance window opens, showing a list of available servers.
    Note that you can also open the Add Server Instance window by choosing Tools > Servers from the NetBeans menu bar and clicking Add Server.
  2. For this tutorial you will use GlassFish V3 Prelude. Select it and click Next.
    Figure 7. Add Server Instance Window
  3. For Server Location, accept the default installation location, or change it if you like. Read the license agreement, click the check box, and download the server by clicking Download V3 Now.
    Figure 8. Downloading GlassFish Server
    Click the image to enlarge it.
  4. When the download is complete, click Finish. You now see the GlassFish application server in the list of available server instances.
    Figure 9. Availability of GlassFish Server
    The GlassFish server will start automatically when you run your application.
Starting and Registering the MySQL Server

To use the MySQL server with the NetBeans IDE, you must start and register it.

To start the MySQL server from the OpenSolaris GUI:

  • From the OpenSolaris toolbar, choose Applications > Developer Tools > Start Apache2/MySQL Servers.
    Figure 10. Starting MySQL Server

To register the MySQL server with the NetBeans IDE:

  1. In the Services tab, right-click the Databases node and choose Register MySQL Server from the menu.
    Figure 11. Registering the MySQL Server
    The MySQL Server Properties window opens.
  2. Accept the defaults for MySQL Server Properties by clicking OK.
    Figure 12. MySQL Server Properties Window

The IDE connects the MySQL server automatically when you run your application.

Creating the Ruby-on-Rails Project

You are now read to create a new Ruby-on-Rails project in the NetBeans IDE.

  1. From the NetBeans IDE toolbar, choose File > New Project.
    Figure 13. Starting a New Project
    The New Project window opens.
  2. In the New Project window, select Ruby from the Categories list. From the Projects list, select Ruby on Rails Application. Click Next.
    Figure 14. Choosing Project Type
    Click the image to enlarge it.
  3. For project name, enter SongList.
    For this project, you will use the built-in JRuby runtime and the GlassFish application server. The NetBeans IDE lets you use a different Ruby implementation and server if you want, such as the Ruby version that you install with the Sun Web Stack.
    Click Next.
    Figure 15. Specifying Project Name, Location, and Ruby Platform
    Click the image to enlarge it.
  4. Select Specify Database Information Directly and make sure the mysql adapter is selected. Click Next.
    Figure 16. Specifying Database Information
    Click the image to enlarge it.
  5. Rails 2.1.0 is bundled with NetBeans 6.5. You will not use SSL, a war file, or a proxy in this tutorial, so accept the settings for the Install Rails step, and click Finish.
    Figure 17. Viewing Rails Installation
    The IDE creates the SongList project, generates the necessary files and Rails directory structure, and lists the directories and files in the Output window as it creates them.
Creating the Database

The NetBeans IDE makes it easy to create a database for your project by running a Rake task. Rake is the Ruby equivalent of make for the C language, or ant for the Java platform.

  1. Right-click the project name in the Projects tab and choose Run/Debug Rake Task from the menu. The Run/Debug Rake Task window opens.
    Figure 18. Choosing Run/Debug Rake Task
  2. In the Run/Debug Rake Task window, select the Rake task db:create from the list. Click Run.
    Figure 19. Choosing the db.create Task
    When you run the db:create Rake task, the IDE creates a development database for you named SongList_development. You will construct a table in this database when you create a scaffold with the Rails generator in the next two steps.
  3. Right-click the SongList project name and choose Generate from the menu.
    Figure 20. Opening the Rails Generator Window
    The Rails Generator window opens.
  4. In the Rails Generator window, you now create a scaffold around a model that you will name song. Make the following changes in the Rails Generator window:
    1. Select scaffold from the Generator drop-down list.
    2. For Model Name, enter song.
    3. For Attribute Pairs, enter artist:string song:string rating:float. The artist attribute will contain the name of the musician, the song attribute will contain the song title, and the rating attribute will rate the song on a scale of 1–10.
    4. Click OK.
    Figure 21. Specifying Scaffold Name and Attribute Pairs
    The NetBeans IDE fills out the Ruby directory structure for the Ruby model-controller-view framework and creates necessary project files, including Ruby source files and an initial database migration class. The generator also creates unit test and fixture stubs for writing model tests. A list of created directories and files is shown in the Rails Generator tab of the Output window.
Migrating the Database

Migration classes, known as Rails migrations, use a wrapping approach to coordinate schema and data changes in the database for your project. You now use a Rails migration to update the database to include the songs table that will hold the attribute pairs.

  1. Right-click the project name in the Projects tab and choose Run/Debug Rake Task from the menu. The Run/Debug Rake Task window opens.
    Figure 22. Choosing Run/Debug Rake Task
  2. Select the Rake task db:migrate. To show only tasks that contain the term "migrate," enter migrate in the Filter field. Click Run.
    Figure 23. Choosing the db.migrate Task
    Click the image to enlarge it.
    The IDE executes database migration scripts. The Output window shows migration activities in the SongList (db:migrate) tab.
Running the Application

Your Ruby-on-Rails project is ready to run.

  1. Click the Run symbol in the toolbar or right-click the project name and choose Run from the menu. Your system default browser, if you have set one, opens to the Ruby welcome page.
  2. Enter the following URL into the browser address field:
    The application's web page opens, showing that there is no song data yet.
    Figure 24. Application Window in Browser
    Click the image to enlarge it.
  3. To add an entry, click the New Song link and fill in the Artist, Song, and Rating fields. The following figure shows the listing after adding three new entries.
    Figure 25. Application Window With Three Songs
    Click the image to enlarge it.

By generating a model, scaffold, and database migration for your project, the NetBeans IDE has created a useful application. Your Ruby application contains no real business logic, but the resulting application provides users with access to a database in which they can enter, display, and delete data.

Now that you have a working project, explore the files that the IDE has created and edit them to change the application behavior. As a simple example, you can change the appearance of the page in the browser by editing the scaffold.css file. To do so, expand the SongList > Public > stylesheets node in the Projects tab, and double-click the scaffold.css file name. Changing the body,  p,  ol,  ul,  td tag styles as shown changes the font and size of the text displayed in the browser.

Figure 26. Application Window With Edited scaffold.css File
Click the image to enlarge it.
Further Information
Rate This Article