Feeds

Ruby runs on Rails with NetBeans

Build a web application in minutes

The Essential Guide to IT Transformation

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

The Essential Guide to IT Transformation

More from The Register

next story
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
KDE releases ice-cream coloured Plasma 5 just in time for summer
Melty but refreshing - popular rival to Mint's Cinnamon's still a work in progress
Leaked Windows Phone 8.1 Update specs tease details of Nokia's next mobes
New screen sizes, dual SIMs, voice over LTE, and more
Another day, another Firefox: Version 31 is upon us ALREADY
Web devs, Mozilla really wants you to like this one
Put down that Oracle database patch: It could cost $23,000 per CPU
On-by-default INMEMORY tech a boon for developers ... as long as they can afford it
Secure microkernel that uses maths to be 'bug free' goes open source
Hacker-repelling, drone-protecting code will soon be yours to tweak as you see fit
Mozilla keeps its Beard, hopes anti-gay marriage troubles are now over
Plenty on new CEO's todo list – starting with Firefox's slipping grasp
prev story

Whitepapers

Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
The Essential Guide to IT Transformation
ServiceNow discusses three IT transformations that can help CIO's automate IT services to transform IT and the enterprise.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
How modern custom applications can spur business growth
Learn how to create, deploy and manage custom applications without consuming or expanding the need for scarce, expensive IT resources.
Build a business case: developing custom apps
Learn how to maximize the value of custom applications by accelerating and simplifying their development.