Tuesday, June 12, 2007

Four Days on Rails by NetBeans

Recently, I have been intent on learning (J)Ruby on Rails. Many books are in bookstores, and so many tutorials are on the Internet. I had a bit hard time what was the good one to start learning. Wondering around the Internet, I found "Four Days on Rails"(http://rails.homelinux.org/) is one of the best tutorials though it is a little bit old. It is based on a command line interface and older version of Rails. However, I successfully got the examples in tutorials work on NetBeans after struggling with both NetBeans and Rails. Here's a memo how I could run the first example and get an output on my browser.

The example ran on...
  • Ubuntu 6.10
  • JDK 1.6.0
  • JRuby 1.0
  • NetBeans 6.0M9
  • Java DB(a.k.a. Apache Derby) included in NetBeans
Before I create Ruby on Rails project, I updated JRuby from 0.9.8 to 1.0 as in my previous blog. I think updating JRuby might be optional, but the latest version would be better because it is the best version so far.
  1. Creating the ToDo project
    • Click File -> select New Project -> click Ruby -> click Ruby on Rails Application -> type "ToDo" in the Project Name box -> click Next -> click the Install Rails button if Rails is not installed and click Close button after Rails is installed -> click Finish
  2. Setting up the database
    • As in the tutorial, "Creating a Ruby Weblog in 10 Minites," edit database.yml and envitonment.rb to make Java DB available from a Rails Application.
    • Goto Projects window and expand Configuration folder -> open database.yml and edit lines after the "development:" so as to be in followings
    • development:
      adapter: jdbc
      driver: org.apache.derby.jdbc.ClientDriver
      url: jdbc:derby://localhost:1527/todos
      username: foo
      password: bar
    • Open environment.rb and add a following snippet before the line "Rails::Initializer.run do |config|" to plug in the JDBC adaptor.
      if RUBY_PLATFORM =~ /java/
      require 'rubygems'
  3. Creating the todos database on Java DB
    • Click Tools -> select Java DB Database -> click Create Java DB Database... -> type the database name, user name, and password in the boxes as follows:
      Database Name: todos
      User Name: foo
      password: bar
  4. Connecting the todos database
    • Go to the Runtime window -> expand Databases -> right-click the todos database -> select Connect...
  5. Creating the category model
    • Go to the Projects window -> right-click the Models node under the ToDo project -> select Generate... -> type the model name, Category, in the box -> click OK
      Arguments: Category
  6. Creating the categories table in the todos database
    • Open the generated file, 001_create_categories.rb, and edit it to be shown below.
      class CreateCategories < ActiveRecord::Migration
      def self.up
      create_table :categories do |t|
      t.column :category, :string, :limit => 20, :default => '', :null => false
      t.column :created_on, :timestamp, :null => false
      t.column :updated_on, :timestamp, :null => false
      add_index :categories, :category, :unique => true, :name => 'category_key'

      def self.down
      drop_table :categories
    • Go to Projects window -> right-click the ToDo project node -> select Migrate Database -> select To Current Version
    • If this completes successfully, the table, categories, is created in the todos database.
    • You might have problems on this stage. For me, restarting NetBeans was the good solution. You can find other solutions on the tutorial, "Creating a Ruby Weblog in 10 Minites."
  7. Creating the script controllers
    • Go to Projects window -> right-click the Controllers node under the ToDo project -> select Generate... -> type the controller name, Script, in the Name box and leave the View box blank -> click OK
      Name: Script
    • Open the generated file, script_controller.rb, and edit it. The following code is the script_controller.rb after editing.
      class ScriptController < ApplicationController
      scaffold :category
    • Expand the Configuration node under the ToDo project -> open route.rb and add following line just before the last "end" line.
      map.connect '', :controller => "script"
    • Expand the Public node under the ToDo project -> delete index.html
  8. Running the application
    • Right-click the ToDo project node -> select Run Project or click the Run Main Project button if the ToDo project is a main one.
    • In the browser, you see the page created by the Rails application.

No comments: