Simple clear advice in plain English

Hands on: Programming with Ruby

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

To fix this, create a file called index.rhtml and save it in the app\views\welcome directory. Files of this type are similar to PHP, ASP or JSP pages, in that they combine plain HTML with server-side script between special tags, as the following (rather contrived) example shows:


PCW App

Welcome to the Games review site


This site covers the following platforms:



    <% for i in [‘XBox’,’XBox 360’,’PlayStation 2’,’PSP’,’DS’]%>
  • <%= i %>

  • <% end %>


Everything between the <% … %> tags is Ruby code executed on the server. The expression <%= asks Rails to output what follows into the HTML.

Variables defined in the controller can also be used in the template. For example, you could insert the following line into the index method:

@title = ‘PCW Games Reviews’

and the following into the section of the template:

<%= @title %>

When you request the page, the value of the variable appears in the output. There is good reason not to put much code into templates. Rails makes it easy to put your code elsewhere; when you generate a Controller, the script creates a Helper module where you can put code for that set of pages.

Hooking up a database
To see the Rails magic, you need to hook up a database to the web application.

By default, Rails uses the open-source MySQL, although there are drivers (called adapters) for several other database servers. On Windows I prefer SQL Server to MySQL, and it is free in the Express version.

Here’s how to use SQL Server 2005.

Both Ruby and Rails come out of the open-source community, and they are geared towards Unix-like operating systems in the same way ASP.Net is geared towards Windows.

Using SQL Server takes a bit of extra effort. First, you have to obtain the file ADO.rb by downloading the source of Ruby-DBI from rubyforge.org. Copy this file to /lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb, within your Ruby directory.

The next step is to create a database. We are making a site for games reviews, so open up SQL Server Management Studio or your preferred SQL Server tool, and create a database called PCW with a table called games.

The table must have an identity field called id, another example of convention over configuration. You can add other fields, such as game, platform, rating and comments. For convenience I set Allow Nulls to false for all the fields.

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.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Virtual drive

A set of files seen by Windows as a separate hard disk.

Great shopping deals from Computeractive