The Register® — Biting the hand that feeds IT

Feeds

Ruby runs on Rails with NetBeans

Build a web application in minutes

Customer Success Testimonial: Recovery is Everything

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).

Agentless Backup is Not a Myth

Latest Comments

Broken...

The Other Steve -

Unfortunately this is an area where things are changing pretty fast. The tutorial uses what's in the current pre-packaged releases of NetBeans 6.0 and the Ruby Pack - in other words this is still version 1.0.x of JRuby and 1.2.3 of RoR. As of now this is still the latest pre-packaged version of the software that comes with NetBeans.

But point taken, in general we try and keep as up to date as possible given time constraints and deadlines...

Pan

0
0

Broken

As since version 2.0, this doesn't work :

class PostController < ApplicationController

scaffold :link

end

Because there's no dynamic scaffolding in newer versions. So if you are using Ruby 2.0 or higher, all the tutorials you read will fail.

When you run the generator, select 'scaffold' rather than model, and type your "Link url:string description:text" into the "Model Name" box. This will generate a controller for you called links_controller.rb (and lots of other files)

Then Right click the project node again and click select Migrate Database > To Current Version. This will create the relevant database table and ruby scripts.

Additionally, routes.rb is under Configuration, not Configuration/Environments as stated, and you don't need to edit it, the scaffold generator did that for you. The url is now ...localhost.../links, although you can always type "Post" in the scaffold generation to change this back.

To add the tags field, right click the Database Migrations node, select Generate, then type "NewTagsField tags:string", OK, (this generates your 002_new_tags_field.rb) and then do the DB mIgrate thing again.

At this point you must either add code, or just regenerate your scaffold using "Link url:string description:text tags:string".

A quick tip for newcomers to the Netbeans IDE, get into the habit of running your project by right clicking the project node and selecting 'Run', because pressing the big green play button will not always run the current project. Netbeans has a few gotchas, but it's worth sticking with it. (Eclipse makes me want to set my own hair on fire.)

I fell heavily into the trap of version differences when I started getting into rails (last week), and it's a shame to see El Reg propagating the misery. Next time, Pan, maybe you could make sure that your tutorial will work on the latest version of the software you're talking about, since this is likely to be the version that newcomers will have installed.

In fairness though, 2.0 was only released in December, and the lack of backwards compatibility is catching a lot of people out.

0
0

Great...

I did a similar tutorial from the netbeans site; it now seems to be a much, much better editor. However, I'd like to see a slightly more advanced tutorial, as there are many, many ones about how to make something that's really simple but useless, and far fewer that actually take it to a useful level.

0
0

More from The Register

SCO vs. IBM battle resumes over ownership of Unix
Zombie lawsuit back and wants to suck the brains out of Linux
Bjarne Again: Hallelujah for C++
Plus: Now officially OK to admit you never used STL algorithms
Interwebs taunt Sir Jony over Apple eye candy makeover
Hey Ive, Ive... add more unicorns, willya?
Apple: iOS7 dayglo Barbie makeover is UNFINISHED - report
Plus: You don't like the icons? Blame marketing
Red Hat to ditch MySQL for MariaDB in RHEL 7
So long, Oracle! Don't let the door hit you on the way out
Shy? Socially inadequate? Fiddling with your phone could help
App 'tells the brutal truth' about social inadequates' chatup lines
Java EE 7 melds HTML5 with enterprise apps
New release arrives with GlassFish, NetBeans support
 breaking news
'Office Facebook' firm Tibbr wants you to PAY for mobe-meetings app
Great idea. Punters won't cough for it though
 breaking news
The only Waze is Google: Ad giant tipped to gobble map app 'for $1.3bn'
Pac-Man-satnav-ish upstart in bidding war with Apple, Facebook
 breaking news
PM Cameron calls for modern, programmable computers! (We think)
IT education musings to G8 chiefs to mystify IT industry