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'
      RAILS_CONNECTION_ADAPTERS = %w(jdbc)
      end
  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
      end
      add_index :categories, :category, :unique => true, :name => 'category_key'
      end

      def self.down
      drop_table :categories
      end
      end
    • 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
      Views:
    • 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
      end
    • 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.

Monday, June 11, 2007

Using JRuby 1.0 on NetBeans

Congratulations on JRuby 1.0 release! Right after the release, I successfully updated JRuby of my NetBeans. Here's what I did.
  1. Install JDK 1.6.0 or later. Ubuntu(and probably other Linux) users *must* use JDK 1.6.0(never use 1.6.0_01) to avoid the problem that some dialog windows of NetBeans don't open.
  2. Download a JRuby 1.0 binary version of archive from http://jruby.codehaus.org/ and extract files.
  3. If you use Java DB for a Ruby on Rails Application, make symbolic link to jdk1.6.0/db/lib/derbyclient.jar in the directory, jruby-1.0/lib or copy it in this directory.
  4. Download Full version of NetBeans 6.0M9 from http://bits.netbeans.org/download/6.0/milestones/latest/ and install it.
  5. Start up NetBeans.
  6. Click Tools in the tool bar, select Options, click Miscellaneous, and open Ruby Installation. Type full path to jruby command or click Browse button and select jruby command from extracted JRuby 1.0 files.
  7. Click Tools in the tool bar, select Ruby Gems, and click Update all button.
  8. Install Rails when you create Ruby on Rails project for the first time by clicking Next button in the dialog window in which you typed the project name. Install Rails button shows up to allow us to click it.
After updating JRuby from 0.9.8 to 1.0, I tried the 10 minutes example introduced at http://www.netbeans.org/kb/60/rapid-ruby-weblog.html. The example worked fine.