Simple clear advice in plain English

Hands on: Programming with Ruby

Use it with Rails and find out how to create your web applications

Ruby is a language that has been around for years, but is getting a lot of exposure now thanks to the web application framework called Rails, along with an increasing appreciation of dynamic languages.

Since both Ruby and Rails (see previous column) are free, there’s no harm investigating whether Ruby might suit you. To get started on Windows, go to rubyforge.org and download the One-Click Ruby Installer for Ruby 1.8.4 or higher.

This is a Windows installer that loads Ruby along with several essential add-ons, including the Rubygems packaging system. You can then use Rubygems to install Rails. Open up a command prompt, switch to the Ruby directory, and enter:

gem install rails –include dependencies

You have to be connected to the Internet for this to work. Another thing to check if you experience problems is the system path, which should include the ruby.exe executable. All going well, gem will go and fetch the latest Rails, currently version 1.1. You can test Rails like this:

rails c:\rails\pcwapp

This command creates a skeleton Rails application at the specified location. Switch to the pcwapp directory and type:

ruby script/server

This starts the Rails application, usually on port 3000. You can then browse to http://localhost:3000. All being well, you will see the Rails welcome page, in which case both Ruby and Rails are installed successfully.

Getting content into Rails
When you create a Rails application, files and folders appear in the specified location. By adding very little code, you can make your application spring into life. First, a word of explanation.

Two key tenets of the Rails philosophy are ‘don’t repeat yourself’ (DRY) and ‘convention over configuration’. An example of this principle is the close relationship between classes, methods and the pages in your application.

To see how this works, open a command prompt, switch to the root directory of pcwapp, and type:

ruby script/generate Welcome

This creates several files, including Welcome_Controller.rb in the app\controllers directory. Edit this file and add a method called index. The code looks like this:

class WelcomeController < ApplicationController
def index
end
end

You have just defined a virtual path in your web application called Welcome, within which there is a page called index.

In other words, the URLs in your application map to the Controller classes and methods. Index is also the default page, so if you navigate to http://localhost:3000/welcome, you will get an error message telling you the template index.rhtml is missing.

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Hands on Spreadsheets - balancing profits

Hands on: The Flickr phenomenon

If you haven’t discovered it yet, find out why this online photo site is causing waves

Hands on: Run your own mail server

With your own network, there’s no need to rely on the hosted facilities of your ISP

Question & Answer

Q.How do I store musician and other information about...

> Read the answer

Q.Why can't my browser find the website address I typed...

> Read the answer

Q.All updates have been downloaded, so why won't Windows...

> Read the answer

Best deals on the web

img

Apple MacBook Pro (MC724LL/A)

£999.99- Buy it now

img

Sony Vaio VPCF23P1E/B

£679.98- Buy it now

img

Samsung 300E5A-A01DX

£449.99- Buy it now

Great benefits for subscribers!

Most popular articles

Poll

Which is your preferred web browser

Jargon Buster

Computing terms explained in plain English

VoIP

Voice over IP. The routing of voice conversations over the internet, which is cheaper than the telephone...

Great shopping deals from Computeractive