Contributed by Chris Kutler
November 2007 [Revision number: V6.0-3]
This document provides information about downloading, installing, and configuring Ruby support in the NetBeans IDE.
If you do not have the NetBeans 6.0 IDE, go to the www.netbeans.org, and download a version of the IDE that contains Ruby support. If you have a NetBeans IDE 6.0 installation that does not include Ruby support, complete the following steps to add Ruby support to the IDE.
Figure 1: Plugin Manager
(Optional) Select the JRuby and Rails Distribution checkbox to download and install the JRuby software and the Ruby on Rails framework.Note: You must have either Ruby or JRuby software installed on your system in order to use the Ruby and Rails distribution. If you do not have this software, you must either install this plugin, or download and install JRuby or Ruby software before working with Ruby projects in the IDE.
(Optional) Select one or more of the following plugins:
Click Install.The NetBeans IDE Installer appears.
The first time that you create or open a Ruby project in the IDE, the IDE checks if you have any other Ruby installations in addition to the bundled JRuby software. If you do, the IDE displays a dialog box asking you to select which software to use.
If you decide to install the Ruby software later, you can complete the following steps to configure the IDE to use your Ruby installation.
Click Ruby, and, if necessary, click the Platform tab.If you have not yet opened or created a Ruby project, the first time that you access the Platform tab, the IDE displays a Choose Ruby Interpreter dialog box. The dialog box shows a list of available Ruby installations, as shown in the following figure. If the dialog box does not appear, skip to Step 5.
Figure 2: Choose Ruby Interpreter Dialog Box
Figure 3: Ruby Options Window
For information about obtaining Ruby software, see www.ruby-lang.org, instantrails.rubyforge.org, and locomotive.raaum.org.
Gems are third-party Ruby libraries. The IDE provides a Ruby Gems manager for adding and maintaining gems. You open the Ruby Gems manager by choosing Tools > Ruby Gems from the main menu. The manager contains the following tabs:
Figure 4: Installed Tab in Ruby Gems Manager
Note. The RubyGems wiki page provides information about how to enable the Ruby Gems manager to manage a native Ruby gems repository.
For more information about Ruby Gems, go to www.ruby-lang.org/en/libraries/. For more information about Rails, go to rubyonrails.org. Information about the ActiveRecord-JDBC gem can be found at jruby-extras.rubyforge.org/ActiveRecord-JDBC.
You can access databases from a JRuby on Rails application in one of two ways.
JDBC Adapter. As mentioned earlier, the JRuby and Rails Distribution plugin includes the ActiveRecord-JDBC gem, which enables your JRuby on Rails application to access database servers that provide JDBC 3.0 compliant drivers, such as MySQL, PostgreSQL, Oracle, HSQLDB, and Java DB (also known as Derby). The JDBC driver must be a pure Java driver. At the time of this writing, ActiveRecord-JDBC does not work with the Sqlite JDBC driver.
To use a JDBC connection, you must obtain a JDBC 3.0 client
driver for your database server, and put a copy of the JDBC
driver in the
folder. Depending on your installation, this folder can be in one of two places.
Note: The MySQL and PostgreSQL JDBC drivers are bundled with the IDE. If your installation includes the GlassFish application server, the Java DB driver is also available. To locate the drivers, expand Databases > Drivers in the Services window. Right-click the node for the driver, and choose Customize. A dialog box opens, which shows the location of the driver file.
To use the JDBC adapter in a Ruby on Rails project, select the Access Database Using JDBC checkbox when you create the project, as shown in the following figure.
Figure 5: Selecting JDBC Database Access
|Code Sample 1: JDBC Environment Setup|
if RUBY_PLATFORM =~ /java/ require 'rubygems' gem 'ActiveRecord-JDBC' require 'jdbc_adapter' end
|Code Sample 2: JDBC Database Configuration Example|
adapter: mysql database: my_development_database username: fred password: secret host: localhost
Note: If your operating system's host file does not contain localhost, use 127.0.0.1 instead. Note also that with some systems, the database setting must be in lowercase letters.
Several of the NetBeans tutorials require that you create a database for the data tables that are used by the tutorial. The steps for adding a database are different for each database server. The following instructions are for the MySQL and Java DB databases.
CREATE USER 'fred'@'localhost'
identified by 'secret';
GRANT ALL PRIVILEGES ON *.* TO 'fred'@'localhost';
mysqladmin -u fred -p create mydb_development
rootas the user name.
For more information about creating MySQL users and databases, see the MySQL Reference Manual.
If you installed a version of the IDE that includes the GlassFish application server, the IDE is configured to access the Java DB database that is provided with GlassFish. You can use the following steps to add a Java DB database from the IDE.
Figure 6: Create Java DB Database Dialog Box
The IDE creates the database and adds a node to the Databases section in the Services window, as shown in the next figure.
Figure 7: Database Node in Services Window
You do not need to register a database with the IDE to use it from your Ruby project. However, by registering a database you can create and view tables, and exercise SQL commands directly from the IDE.
First, you must register the database server with the IDE. The IDE has been tested with the following drivers:
MySQL, PostgreSQL, and, optionally, Java DB are already registered with the IDE. The following steps show how to register other database servers with the IDE.
In the next step, you specify the path to this driver. The IDE uses the JDBC driver to access the database schema and to query the databases. If the database client driver is not fully JDBC 3.0 compliant, you might have problems using the IDE's database tools.
Click OK to close the New JDBC Driver dialog box.The IDE adds a node for your database server under the Drivers node.
The following steps show how to create a connection from the IDE to a database instance, such as the example mydb_development database described in the previous section.
Figure 8: New Database Connection
When the connection is established, click OK to dismiss the dialog box.The IDE adds a node for the connection under the Databases node. The badge for the node shows that the connection is open. When you next start the IDE, the badge will be broken to indicate that the connection is closed. To reconnect, right-click the node and choose connect, provide your password if it is required, and click OK.
By default, your JRuby applications run in the WEBrick browser. If you want to deploy a JRuby application to the GlassFish application server, you can package the application and its dependencies in a web archive (WAR) file. Then you can make the WAR file available to the application server, as shown in the following steps.
Figure 9: Option to Provide WAR Rake Targets
If you do not see an entry for goldspike, complete the following steps to register the repository that provides the goldspike plugin.
To learn how to use the IDE to quickly build a Ruby on Rails application, go to Creating a Ruby Weblog in 10 Minutes.