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
- 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
- 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
- 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 - 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 - Connecting the todos database
- Go to the Runtime window -> expand Databases -> right-click the todos database -> select Connect...
- 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
- 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."
- 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
- 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.
development:
adapter: jdbc
driver: org.apache.derby.jdbc.ClientDriver
url: jdbc:derby://localhost:1527/todos
username: foo
password: bar