Ruby runs on Rails with NetBeans
Build a web application in minutes
NetBeans and Ruby, part 2 Having limped along in the wake of Eclipse, Sun Microsystems' NetBeans is getting its second wind. One thing that's helping is a fresh focus on scripting, particularly around Ruby on Rails.
In this piece, I shall show how well Ruby on Rails has been integrated into NetBeans using the creation of a very simple application as an example.
I'm going to build a simple link sharing application - which we'll call Linkshare - along the lines of Reddit. The functionality is straightforward: users can post interesting links to the site. Links can be displayed, updated and deleted: in others words, perform the most basic of database procedures on these posts.
For our backend database we'll use MySQL. So, before we kick off any of the Rails stuff, log on to your database server and create a database called Linkshare. And, for convenience create a user called ls. We'll leave the password blank for this tutorial.
We can use NetBeans to connect to this empty database to check that we can connect to the server and to see what state the database is in. Click on the Services tab in NetBeans, expand the Databases tree, right click on Drivers and select the MySQL node, then right click and select Connect Using on the New Connection dialog, enter the database URL and the user name, and hit OK. NetBeans will make the connection, and once it's done you can expand the tree still further to view Tables, Views and Procedures (which should be empty).
The next step is to create the Linkshare project. File New starts the new project wizard and clicking on the Ruby category gives us a number of choices, including Ruby on Rails application. Clicking Next allows us to give the project a name - Linkshare - and to pick which RDBMS we want to use. When we click on the Finish button, Rails will do its stuff and generate the entire application structure for us.
Once that's in place, we need to tell the project how to connect to the database. From the Projects tab expand the Linkshare project, navigate to the Configuration node and then double click on the database.yml file to open it in the editor. Change the development database section so that it matches your connection settings:
development: adapter: mysql database: linkshare username: ls password: host: <your host name or IP address>
Now, a major part of the Ruby on Rails architecture is an implementation of the Model-View-Controller (MVC) and ActiveRecord design patterns. If you're unfamiliar with the terminology don't worry, Ruby on Rails makes things simple for you. The model is a fancy way of talking about the database. And we'll add to ours by creating a table called links to store all of the links that users are going to post to our site.
Right click on the Linkshare node in Projects, select Generate and from the drop-down menu pick Model. For each link our users enter we want the URL and some description, so for the Arguments field we specify:
Link url:string description:text
When we hit OK, Rails does more application generation, including the creation of a script to set up and tear down the database table for us. The file that's generated is called 001_create_links.rb, and looks like this:
class CreateLinks < ActiveRecord::Migration def self.up create_table :links do |t| t.column :url, :string t.column :description, :text end end def self.down drop_table :links end end
To run this, right click on the Linkshare node again, select Migrate Database > Current Version to connect to the database and create the links table. Once that's done, switch to the Services tab, select the Database connection and hit Refresh, click on the Tables node to see that there is indeed a links table, with fields for URL and description (as well as an ID field that was automatically added for us).
Next, we want to wire this up to a web page that allows us to add, edit and delete entries (which we'll refer to as posts) to this database. For that we need a Controller. Again, right click Linkshare, select Generate and this time use the drop-down menu to select Controller. Enter a value of Post for the Name field and then OK. This generates a Ruby file called post_controller.rb, which opens in the editor.
We add a single line of code to the skeleton so that it looks like this:
class PostController < ApplicationController scaffold :link end
The final thing is to make sure that requests are routed to the correct place, so edit the routes.rb file (which is under the Configuration > Environments node of the Linkshare project tree). Adding the following line should route requests to the post controller:
map.connect ' ', :controller => "link"
With that in place, press F6 (or click on the big green Run Main Project button on the main tool bar) and launch the WEBrick web server with our Ruby on Rails application in place. Opening a browser and entering the URL http://localhost:3000/post will take us to a very basic looking web page that enables us to add, edit, list and delete posts.
Want to do more? Say, we decide to add a tags field so we have an idea what each URL is about. Easy. In the NetBeans Generator, menu select Migration, enter "new_tags_field" and then hit OK. This creates a new class called 002_new_tags_field.rb and opens the file in the editor. We'll make use of one of NetBeans keyboard triggers to make life simple for us. Enter a new line under def self.up, then enter mcol and then press the tab key. This expands to:
add_column :table, :column, :string
Change this to:
add_column :links, :tags, :string
Save the change, run the Migrate Database command to add a new tags field to the links table. Hit Refresh in the browser, click on the New Link button and, hey presto, there's a tags field as well.
It might not look like much, but we have the skeleton of a useful web application, created in just a few minutes. Enhancing the application with validations, adding new fields or functions (like voting buttons), or adding some better formatting can all be done with a few key strokes or mouse clicks.
It's why Ruby on Rails has made such a splash, and shows how well Ruby on Rails has been integrated into NetBeans.®