NBDemoRubyOnRailsInvokingJava

Ruby on Rails using JRuby Demo (10 Minutes) (Work In Progress...)

Description

This Ruby on Rails demo that shows how to create a Rails application that uses MySQL and also calls a Java library using JRuby

Product Versions Supported

NetBeans 6.0 Beta 1 and higher

Points to Hit

Demo Prep

The demo steps assume you are using CRuby and MySQL. CRuby was chosen because the following debugging demo does not yet work well on JRuby. If necessary, the steps could be augmented to work with JRuby (basically the database connectivity).

  1. Install MySQL Database Server Community Edition from http://dev.mysql.com/
  2. Create a connection to the mysql database (jdbc:mysql://localhost/mysql,userid=root). From this connection you can easily execute commands to create other databases.

Gotchas

None known at this point

Demo

  • Create a new Ruby on Rails Application named customers
  • Create a new database named customers
  create database customers;
  • Edit the database.yml file (under Configuration)
  • make sure the database: on line 16 is "customers"
  • specify the MySql username/password on lines 17 and 18. These should be the same as the username/password used to create the database
  • Create a model named Customer (Right-click the project and select Generate. Select model and set the arguments to Customer).
  • Review the Output window for what was created: the model class, a unit test, a test fixture and a migration. This presentation doesn't have time to get into testing.
  • Open the "Database Migrations->migrate->001_create_customers.rb" migration and add the following to the create_table method:
            t.column :name,         :string
            t.column :description,  :text
           
  • Save the file. Right-click the project choose Migrate Database > To Current Version
  • Generate a controller to work with the data table. Generate > Controller > Customer
  • In the CustomerController, which is now open in the Editor add the following. A Rails scaffold is an dynamically generated (remember metaprogramming) framework for manipulating a model:
      scaffold :customer
  • Press Shift+F6 to save, start the server and open the browser to: http://localhost:3000/customer
  • Click the New customer button. Add a customer and then save it.


Generate the Views So They Can Be Modified

Currently, the scaffolding is auto-generating our views using metaprogramming. Since we ultimately want to customize the look of our application, let's generate the view and controller code for our app.

  • Generate a scaffold, setting the Model Name to customer and the Controller Name to Customer. Be sure to select the Overwrite option as the Customer controller already exists.
  • The application will behave the same, but all of the code that was being dynamically generated for us is now part of our controller and the views have also been generated.
  • Show the generated code in the CustomerContoller as well as the views that were created.


Demo Cleanup

These are the necessary steps to take in order to successfully run the demo again on the same machine.

  1. Migrate the database to version 0.
  2. Delete the customers project
  3. Delete the customers database
  drop database customers;
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