|
| 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:
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.
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.
This tutorial uses the MySQL RDBMS. The community version is available for download at no cost.
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.
| |
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:
Figure 1.
Package Manager Menu
|
Figure 2.
Package Manager Window
|
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:
Figure 3.
Initializing Sun Web Stack
|
| 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.
| |
Start the NetBeans IDE by choosing Applications > Developer Tools > Netbeans IDE from the menu bar.
Figure 5.
Starting the NetBeans IDE
|
| |
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:
Figure 6.
Adding a Server Instance
|
Figure 7.
Add Server Instance Window
|
| Figure 8.
Downloading GlassFish Server
Click the image to enlarge it. |
Figure 9.
Availability of GlassFish 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:
Figure 10.
Starting MySQL Server
|
To register the MySQL server with the NetBeans IDE:
Figure 11.
Registering the MySQL Server
|
Figure 12.
MySQL Server Properties Window
|
The IDE connects the MySQL server automatically when you run your application.
| |
You are now read to create a new Ruby-on-Rails project in the NetBeans IDE.
Figure 13.
Starting a New Project
|
| Figure 14.
Choosing Project Type
Click the image to enlarge it. |
SongList.
| Figure 15.
Specifying Project Name, Location, and Ruby Platform
Click the image to enlarge it. |
| Figure 16.
Specifying Database Information
Click the image to enlarge it. |
Figure 17.
Viewing Rails Installation
|
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.
Figure 18.
Choosing Run/Debug Rake Task
|
db:create from the list. Click Run.
Figure 19.
Choosing the db.create Task
|
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.
Figure 20.
Opening the Rails Generator Window
|
song. Make the following changes in the Rails Generator window:
scaffold from the Generator drop-down list.
song.
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.
Figure 21.
Specifying Scaffold Name and Attribute Pairs
|
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.
Figure 22.
Choosing Run/Debug 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. |
SongList
(db:migrate) tab.
| |
Your Ruby-on-Rails project is ready to run.
http://localhost:8080/SongList/songs |
| Figure 24.
Application Window in Browser
Click the image to enlarge it. |
| 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. |
| |
| |
| |
