Note: Development of Ruby support ended with NetBeans IDE 6.9. However, the NetBeans Ruby community is strongly encouraged to take on development of NetBeans IDE Ruby and Rails support. See the NetBeans Ruby Community Wiki for information.
This tutorial gives you a quick tour of Ruby on Rails application development in NetBeans
IDE. The tutorial shows you how to use the IDE's Ruby on Rails support to perform some typical
application development tasks.
To make this tutorial quick and easy, the tutorial does not work with a database. If you
are interested in learning about how to build a Rails database application, see the
Creating a Ruby Weblog in 10 Minutes tutorial.
In addition to Ruby projects, NetBeans Ruby support enables you to work with Ruby on Rails
projects. Rails is a framework that enables you to quickly create database-backed web applications that are based on the
model-view-controller (MVC) architecture.
You create a Rails project by right-clicking a blank spot in the Projects
window and choosing New Project from the pop-up menu. You select Ruby from the Categories pane, and you
select either Ruby on Rails Application or Ruby on Rails Application With Existing Sources from the
Projects pane. When you are creating a project from existing sources, Step 2 of the New Project wizard
enables you to name the project and specify its location.
Just as with Ruby projects, you can select the Ruby platform
from the drop-down list. If you have installed a platform that
does not appear on the drop-down list, click Manage to add
that platform.
By default, the IDE sets up the project to use three MySQL databases,
which are given the names project_development,
project_test, and
project_production, where project is
the name of your Rails project. To change the default configurations,
you can edit the database.yml file, which opens in
the editor after you create the project. Alternatively, you can
click Next in the Project wizard to display the Database Configuration
page, which is shown in the following figure.
You can change a project's settings by right-clicking
the project node and choosing Properties from the pop-up menu.
The Project Properties dialog box,
which is shown in the following figure,
enables you to configure the project's platform, server, mode,
and encoding, and to specify arguments to the rake command.
You can also create different configurations
for a project, and use this dialog box to switch
configurations.
Try It
Follow these steps to create a Ruby on Rails
project.
Right-click a blank spot in the Projects window and choose
New Project from the pop-up menu.
In the New Project wizard, select Ruby in the Categories pane,
select Ruby on Rails Application
in the Projects pane, and click Next.
Name the project, for example,
simple_rails_application.
Click Finish.
This tutorial does not use a database, so you
do not need to perform any database configurations.
Examine the logical view of the file structure in the Projects
window.
Right-click the project's node (the root node for the project)
and notice the menu options.
Click the Files tab and compare this physical file structure
with the logical view that is presented in the Projects window.
The following figure shows the windows side by side,
to make it easy to compare the two views.
To have the Projects window display
the physical layout instead of the logical layout,
choose Tools > Options
from the main menu, and click Miscellaneous.
Click the Ruby tab, clear the Show Logical Project
View checkbox, and restart the IDE.
Right-click the project's node in the Files window.
Notice the different menu options, compared to the pop-up menu in the Projects window.
For example, the pop-up menu for the project's node in the Projects window provides the Generate action,
Run/Debug Rake Task action, and the Rails Console action, in addition to many other Rails specific actions.
For More Information
For more information about database configuration from the Project Wizard, see
Using Database Servers With JRuby , and the New Ruby on Rails Application Wizard: Database
Configuration help topic. To quickly access the help topic, type database in the main toolbar's search
box.
Working With Ruby on Rails Files
Note: This section is written for
Rails 2.3
Just as with Ruby projects, you can open a file in the editor by double-clicking the file's node in either the Projects
window or the Files window. Alternatively, you can press Alt-Shift-O (use Ctrl-Shift-O on the Mac) to access a file by name.
The pop-up menus for the nodes in the Project window provide easy access to Rails scripts and Rake tasks, such as
the generate script for generating code, and the db:migrate task for migrating to
a specific version of the database tables.
The IDE “understands” the relationships between the file types, and makes it easy to navigate
to associated files. For example, if you are editing a view file, you can use the pop-up
menu to navigate to the associated action file or test.
As with all NetBeans projects, you can run your
application by clicking the Run Project button,
or by right-clicking the project's node and
choosing Run from the pop-up menu. The
IDE saves all file changes, starts the web server, if necessary,
then displays the application's
start page in your browser. You can also use the
Run File menu action in the editor to open in
the browser the relevant URL to the
controller, action, view, or helper that you are editing.
Try It
Complete the following steps to create a Rails version
of the sample project presented in the
Working With Ruby Files section. In this variation,
the constructor takes a hash instead
of positional arguments, and
you obtain the data from a
YAML file.
Note: Typically, with a Rails project, you base your model classes on database tables. However, to
make this example quick and simple, the application obtains its data from a YAML file. To learn how to use
the IDE to build a Rails database application, see the Creating a Ruby Weblog in 10 Minutes
tutorial.
Create the Model Class
Create a Ruby on Rails project, or use the one that you created in the previous section.
In the Projects window, expand Configuration, and double-click environment.rb to open the file in the editor.
Scroll down to the following comment:
To show line numbers Right-click in the left margin of the editor
and choose Show Line Numbers.
# Skip frameworks you're not going to use (only works if using vendor/rails).
# To use Rails without a database, you must remove the Active Record framework
# config.frameworks -= [ :active_record, :active_resource, :action_mailer
Uncomment the third line, as shown in the following code.
# Skip frameworks you're not going to use (only works if using vendor/rails).
# To use Rails without a database, you must remove the Active Record framework
config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
You can click the Uncomment button (
)
to uncomment selected lines.
Following the Rails principle of
convention over configuration,
a Rails 2 application tries to access the database that
is specified in the database.yml file. Because
this application is not using a database, you must remove the
Active Record framework from the environment configuration
so that the application does not try to access a database.
In the Projects window, right-click the Models
node and choose New Ruby Class from the contextual menu.
The Rails Generator dialog box opens, with model
selected in the Generate drop-down list,
as shown in the following figure.
Type Item in the Arguments text box and
click OK.
The generator creates a file named item.rb and opens
the file in the editor. A node for the file appears under
the Models node in the Projects window. By default,
the generator creates a
test suite under
Unit Test, a test fixture under Test Fixtures, and a migration
under Database Migrations > migrate.
Replace the contents of the item.rb file with
the following code.
# _Item_ # # Takes: # :id => unique item id # :type => type of item # :price => price of the item classItem DATA_FILE = RAILS_ROOT + "/data.yml" attr_accessor :id, :type, :price
def to_s "Item #{@id} is a #{@type}: Price $#{@price}" end # Returns an array of all the items sold in the store. defself.load_item_data YAML.load_file(DATA_FILE).collect do |item_hash| Item.new(item_hash) end end end
Generate the Documentation
Right-click the project's node and choose Run/Debug Rake Task
from the pop-up menu.
Type doc in the Filter text box.
The Matching Tasks list narrows down to all
the Rake tasks that
include doc in their name, as shown in the
following figure.
Double-click the entry
for doc:app.
The IDE runs the doc:app
task and places the output in
Documentation > app. It might take
a while for the app node to appear
under Documentation in the Projects window.
To see the documentation for the
Item class, right-click Documentation >
app > classes > Item.html
and choose View from the pop-up menu.
Create the Data File
The Item class requires the data.yml
file for its data. To create this file,
right-click the project's
node in the Projects window, and choose New > Other from
the pop-up menu.
In the New File dialog box, select Ruby
in the Categories pane,
select YAML File in the File Types pane, and click Next.
Type data in the File Name text box
and click Finish.
The IDE creates a file named data.yml
in the project's root
folder and opens the file in the editor. The file's node
might not immediately appear in the Projects window.
Replace the contents of the data.yml
file with the
following text.
-
id: BF15678
type: book
price: 25.32
-
id: C29589
type: cd
price: 18.95
-
id: F89028
type: beverage
price: 2.00
-
id: BN98232
type: book
price: 45.33
-
id: BF15890
type: book
price: 15.98
Create the Controller and View
The model is ready. Now add the controller and the view.
In the Projects window, right-click the
Controllers node and choose
Generate from the pop-up menu.
The Rails Generator dialog box opens with controller
selected in the Generate drop-down list,
as shown in the following figure.
Type Item in the Name text box, type
index in the Views text box, and click OK.
The generator creates both the ItemController class
and the index.html.erb view, which is under the
Views > item node. In addition, the generator creates
Test Files > functional > item_controller_test.rb and
Helpers > item_helper.rb.
Replace the contents of the item_controller.rb file
with the following code.
classItemController < ApplicationControllerdef index
@items = Item.load_item_data
endend
The index action, which the controller
calls before calling the index view,
fills the @items global array
with the list of items.
To quickly access the index.html.erb file, right-click
a line in the index definition, and choose Navigate > Go to Rails
Action or View from the pop-up menu, as shown in
the following figure.
Replace the contents of index.html.erb with
the following markup.
<h1>List of Items</h1>
<table border="1">
<tr><th>Id</th><th>Type</th><th>Price</th></tr>
<% for item in @items %>
<tr>
<td><%= item.id %>
</td>
<td><%= item.type %></td>
<td align="right"><%= number_to_currency(item.price) %></td>
</tr>
<% end %>
</table>
The Ruby code that is embedded in the HTML iterates
over the @items global array
that was defined by the index action in the controller.
Run the Application
Click the Save All button in the main toolbar to save all your
changes.
The asterisks (*) in the file tabs, which indicate modified files,
are no longer displayed.
Right-click inside index.html.erb and choose Run File from the pop-up menu.
The IDE sends the URL for the item controller and the index
action to the server, which in turn sends the following
page to the browser.
Run the whole application.
Note that the standard Ruby on Rails welcome page
appears. This is because
the router, by default, displays the Public >
index.html file. You
change the routing in the following steps.
In the Projects window, expand Public.
Right-click the index.html node and choose Delete from
the pop-up menu.
In the Projects window, expand the Configuration node and
double-click routes.rb to open it in the editor.
Look for the following comment.
# map.root :controller => "welcome"
Replace the comment with the following code.
map.root :controller => "item"
To ensure that the server implements the routing changes,
click the server stop button that appears in in
the left margin of the Server's tab in the
Output window, as shown in the following
figure.
Right-click the project's node and choose
Run from the pop-up menu
to start the application in the browser.
To practice what you have learned, create another Rails project.
Have the project read and display the entries in a task list.
The IDE provides a sample Rails application. To access the sample application, select File >
New Project from the main menu. Expand Samples in the Categories pane, and select Ruby. Select Depot
and click Next. Click Finish and follow the instructions in the README that appears in the browser.
To obtain support and stay informed of the latest changes to the NetBeans Ruby development features,
visit the NetBeans Ruby Community Page
and join the
mailing list.
The Ruby on Rails web site contains screencasts, presentations, tutorials, and samples.