Revision as of 18:30, 6 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

Contributed by John Kostaras
August 2009

This tutorial is an update of IntroductionToRubyOnRails that uses the latest version of Netbeans (6.7) and Ruby on Rails 2.3.3.

The new Netbeans 6.7 IDE contains full support for Ruby and Ruby on Rails. Ruby on Rails is an exciting new framework that allows for rapid web development based on the powerful Ruby scripting language.

In this article I demonstrate how you can develop in Netbeans 6.7 the example found in the book Rails for Java Developers (see the References).

With Rails you can build a simple web application nearly instantly, using the scaffold code generator. This tutorial will walk you through creating a functioning web app in about fifteen minutes. You’ll create a simple, form-based CRUD application for creating, reading, updating, and deleting people. I won’t explain the steps in detail here, even though most of them are straightforward. The interested reader may refer to the book Rails for Java Developers which explains each aspect of the following code example in more detail.

Create the Ruby on Rails Project

As Netbeans 6.7 comes already with support for Ruby and Rails, there is nothing else to do, than, off course, start the IDE. Click on the New Project button and select the Category Ruby and Project Ruby on Rails Application as shown in the following figure:

If we didn't use Netbeans, we would have given the following command to the command prompt or shell:

   > rails people

However, using Netbeans, we provide little more information. Fig.1

Click on Next. We shall create a Rails application named people. Edit the project name in the next screen as shown in the figure below (providing your own path for the project location):


You may also select JRuby instead of the native Ruby platform from the combo box. You may also select as a server either Mongerl or WEBrick. Click on Next to configure the database to use. In this tutorial we shall use the already installed in all platforms, sqlite3 DBMS. Modify the database name as shown in the following figure and click on Next:


In the next screen select the latest version of Rails (2.3.3) and click on Finish.


Netbeans has created a number of folders under people folder. This is a Rails project. Rails applications use the Model-View-Controller or MVC design pattern. In short, this design pattern decouples the presentation layer or web pages (View) from the business logic or database layer (Model) and bridges the two via Controller. You have probably already spotted that folders named Models, Views and Controllers have already been created by the Rails application. Additionally, a Helpers folder has been created as well as various folders for testing and logging, everything a modern developer needs. Agile development straight away.

Create the Scaffold

And here comes the next step, to generate a scaffold. Right click on the people project and select Generate. Select scaffold from the combo box and add Person to Model Name; this will create our Model class. Fill in the Attribute Pairs as shown in the following figure; these will create the database fields. Click OK. No need to add a Controller name. Rails does a good work and will provide people as the controller name for you!

The same result, without Netbeans, can be achieved by typing the following command to the shell:

  > ruby script/generate scaffold Person first_name:string last_name:string age:integer

Rails is working:

    exists  app/models/
    exists  app/controllers/
    exists  app/helpers/
    create  app/views/people
    exists  app/views/layouts/
    exists  test/functional/
    exists  test/unit/
    create  test/unit/helpers/
    exists  public/stylesheets/
    create  app/views/people/index.html.erb
    create  app/views/people/show.html.erb
    create  app/views/people/new.html.erb
    create  app/views/people/edit.html.erb
    create  app/views/layouts/people.html.erb
    create  public/stylesheets/scaffold.css
    create  app/controllers/people_controller.rb
    create  test/functional/people_controller_test.rb
    create  app/helpers/people_helper.rb
    create  test/unit/helpers/people_helper_test.rb
     route  map.resources :people
dependency  model
    exists    app/models/
    exists    test/unit/
    exists    test/fixtures/
    create    app/models/person.rb
    create    test/unit/person_test.rb
    create    test/fixtures/people.yml
    create    db/migrate
    create    db/migrate/20090823082929_create_people.rb

Notice how the model file is named person but the fixture file (which may contain more than one person) is named people. Rails works hard to sound like the way people talk and automatically uses the singular or plural form of words where appropriate.

You may view the class Person in Models:

class Person < ActiveRecord::Base

The wizard has created the following file (Database Migrations-->Migrate-->20090823082929_create_people). (The timestamp in the filename will be different in your case):

class CreatePeople < ActiveRecord::Migration
  def self.up
    create_table :people do |t|
      t.string :first_name
      t.string :last_name
      t.integer :age


  def self.down
    drop_table :people

These are the Attribute Pairs we added in the scaffold wizard.

Create the database

Next step is to actually create our database, or databases. Netbeans has opened the file database.yml which is located under Configuration. Rails uses 3 databases, one for testing, one for development and one for production. Following the book, you will create only two databases, people_development and people_test, so edit the above file accordingly by removing the reference to the production database and editing the database names to add people in front of the name.

# SQLite version 3.x
#   gem install sqlite3-ruby (not necessary on OS X Leopard)
  adapter: sqlite3
  database: db/people_development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
  adapter: sqlite3
  database: db/people_test.sqlite3
  pool: 5
  timeout: 5000

Right click on the project folder and select Run/Debug Rake Task. Select the db:migrate rake task and click on OK. Rake is an automation tool similar to Java’s Ant or Maven.
The same result, without Netbeans, can be achieved by typing the following command to the shell:

  > rake db:migrate


Rails is working:

(in /Users/john/Projects/netbeans/RoR/people)
==  CreatePeople: migrating ===================================================
-- create_table(:people)
   -> 0.0024s
==  CreatePeople: migrated (0.0026s) ==========================================

To view the database, you may wish to download SQLite Database Browser. Click on Open and select db\people_development.sqlite3.


You should see a table people with 3 fields, first_name, last_name and age.

Run the application

  1. In the Projects window, expand the Configuration node and double-click routes.rb to open it in the editor.
  2. Find the following comment.
 map.root :controller => "welcome"
  1. Uncomment the line and change the controller to "people", as shown next.
 map.root :controller => "people"
  1. In the Projects window, expand the Public node, right-click index.html and choose Delete from the pop-up menu.
The index.html page displays a default Welcome page, which is not what you want. By deleting index.html, Rails looks in routes.rb to determine which page to display.
  1. Choose File --> Save All from the main menu.

That's it. Right click on people project and select Run. Mongrel, the builtin Ruby web server, is automatically started on project creation (as well as on Run, if not already running). There is a Mongrel console window which shows the output of the web server, like the requests it has processed.

There is no deployment step - the application is already deployed in place. Your favorite brower starts pointing to http://localhost:3000/people and voilà, you have a full web application with very few steps. You should see a simple, form-based application for creating, reading, updating, and deleting people.

Try it for a few minutes, and make sure everything is working. The scaffold isn’t pretty, but it provides a lot of functionality for little work. If you review the steps you just went through, you have a web application up and running with zero lines of handwritten code!

Without Netbeans, you would start the web server by typing:

  > ruby script/server


Let's run the automated tests for your application. Right click on people and select Run/Debug Rake Task --> test. Even if we didn't write any tests, when you executed scaffold, Rails created some for you. Rails helps you test your project by putting testing in place on day one. Rails is primarily about generating code.

In Rails, the development cycle is carefully designed to minimize interruption. You change your code and refresh your browser to see the changes. That’s all. There is no compile, deploy, or server bounce necessary. This has two advantages: faster development and you learn as you go.

Extending our basic web application

Let's add some data validation to our People application. If you create a person with an empty first name and last name, it will happily store a bogus record in the database. Validation is critical to web applications, and Rails makes validation simple. To add validation, edit the file people/Models/person.rb like so:

class Person < ActiveRecord::Base
  validates_presence_of :first_name, :last_name

The validates_presence_of part requires that both the first name and the last name be present, i.e. not be nil. Navigate your browser to http://localhost:3000/people/new, and try to create a person with no name. When you click on Create, you will see an error message:


When you add validations to a model, their effects automatically propagate to the view, with no additional work necessary.

Finally, let's add a search box in the list view. Edit file people/Views/layouts/index.html.erb and insert the following code right after

Listing people


<%= start_form_tag('', :method=>'get') %>
  People named:
  <%= text_field_tag 'search', @search %>
  <%= submit_tag 'Find'%>
<%= end_form_tag %>

This only causes a search box to appear in the list view. Open file people/Controllers/people_controller.rb and find the list method:

def list
  @person_pages, @people = paginate :people, :per_page => 10

If the user specifies no search term, the method should continue to work as is. If there is a search term, we will compare against both first and last names. Replace it with this expanded version (from Rails for Java Developers book):

def list
  @search = params[:search]
  if @search.blank?
     @person_pages, @people = paginate :people, :per_page => 10
     query = [first_name=:searchOrLast_name=:search,{:search=>@search}]
     @person_pages, @people = paginate :people, :per_page => 10, :conditions=>query


Now, run your application, refresh your view of http://localhost:3000/people/list, and add a few people if you haven’t already. Then try some search terms. The list is updated to show only the matching names.

As is so often the case with Rails, we didn’t have to do anything to test our changes, other than refresh the browser. Our changes themselves were minimal and to the point. We didn’t have to tell Rails how to convert URLs into controller methods, how to connect models to views, or how to find the right view for a controller action. Almost everything in Rails has a default, and you need configuration only when you want to override the defaults.

That's it. This was the sample application from the book Rails for Java Developers, implemented using Netbeans 6.0. As you saw, Netbeans 6.0 provides a very good support for Rails and makes Rails development even easier.


  1. Halloway S. & Gehtland J. (2007), Rails for Java Developers, The Pragmatic Programmers.
  2. Leonard B. & Kutler C., Creating a Ruby Weblog in 10 Minutes
  3. Kostaras J. (Dec 2007), Introduction to Ruby on Rails
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo