Feeds

Ruby runs on Rails with NetBeans

Build a web application in minutes

Choosing a cloud hosting partner with confidence

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

Internet Security Threat Report 2014

More from The Register

next story
Download alert: Nearly ALL top 100 Android, iOS paid apps hacked
Attack of the Clones? Yeah, but much, much scarier – report
NSA SOURCE CODE LEAK: Information slurp tools to appear online
Now you can run your own intelligence agency
Whistling Google: PLEASE! Brussels can only hurt Europe, not us
And Commish is VERY pro-Google. Why should we worry?
Microsoft: Your Linux Docker containers are now OURS to command
New tool lets admins wrangle Linux apps from Windows
Soz, web devs: Google snatches its Wallet off the table
Killing off web service in 3 months... but app-happy bonkers are fine
First in line to order a Nexus 6? AT&T has a BRICK for you
Black Screen of Death plagues early Google-mobe batch
prev story

Whitepapers

10 ways wire data helps conquer IT complexity
IT teams can automatically detect problems across the IT environment, spot data theft, select unique pieces of transaction payloads to send to a data source, and more.
Why CIOs should rethink endpoint data protection in the age of mobility
Assessing trends in data protection, specifically with respect to mobile devices, BYOD, and remote employees.
A strategic approach to identity relationship management
ForgeRock commissioned Forrester to evaluate companies’ IAM practices and requirements when it comes to customer-facing scenarios versus employee-facing ones.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Business security measures using SSL
Examines the major types of threats to information security that businesses face today and the techniques for mitigating those threats.