Ruby on Rails in 10 lines page 1

Ian Piper

On to page 2 | On to page 3 | On to page 4

Rails has created a huge amount of interest amongst web developers in the last few years, but in many parts of the web world it is seen as having such a steep learning curve that it is not worth the trouble. Actually there are many other reasons people don't look into Rails as a development environment, but without a doubt the perception of difficulty is one of the main stopping points.

One of the problems in understanding Rails is confusion between Ruby and Rails. Ruby is a programming language, the nouns and verbs used to create the functions and commands that drive a web application. Rails provides the architecture, the framework, the structure of the application. You can use Rails-like frameworks with other programming languages.

And really, it isn't hard at all. Ruby, the language under Rails, is simple to learn, clear and elegant. For the Rails framework itself the learning curve is not that steep, and the beauty of Rails is that you can quickly learn just what you need to get real usable results no matter what your level of understanding. In other words, you don't have to learn everything about it to get productive - it's Just In Time Learning!

Rails embodies the concept of agile development, and a cornerstone of that approach is frequent delivery of real working code. The result is rapid gratification for your efforts. Many developers are amazed at how much you can get done in Rails with very little effort, and I have got more than one friend hooked into The Rails Way with the demonstration in this article. Take a look at how quickly you can produce a real working web application that provides all of the basic functions you need to create, and manage data.

So, here it is - the ten-line web application. Amaze your friends. Confound your enemies.

There is a gotcha

This article assumes that you have installed Rails v2.2 (current at the time of writing) on your computer. It isn't difficult, and there are many guides on the web to tell you how, so I'm not going to cover this here.

A quick run-through

We're going to go through this at a gallop, and then come back and examine what we've done line by line. Start by opening up a terminal or command-line program. Get to a suitable folder where you are happy to create your new application. It really doesn't matter where, as long as you are able to create files and folders there. Now, type in the following lines, one after another:

rails addressbook

cd addressbook

script/generate scaffold person name:string address:string phone:string age:integer

rake db:migrate

script/server

Now, open a new terminal or command line window, move to the folder addressbook/app/models, and open the file person.rb in a text editor. How you do this, which way round the "/" or "\" characters go and so on, will depend on your computer. The commands you see here are those you would use on a Mac or Linux box: I am using the brilliant Mac-based TextMate editor here, hence the command "mate".

cd app/models

mate person.rb

Now you are in an editor, add the two lines highlighted in bold below and save.

class Person < ActiveRecord::Base

validates_presence_of :name, :address

validates_numericality_of :age

end

Finally go to this url...

http://localhost:3000/people

...and gaze in awe at what you have wrought.

It may not look all that visually appealing, but your application as it currently stands does this:

 

 

Try it: create a few records, edit, make some deliberate mistakes, and you will see just how much you have achieved in less than ten lines of code.

Now, this is all very well, and maybe it strikes you as a nice trick but not a real application. For one thing it looks pretty dull. That is another strength of Rails. When you create an application Rails makes some assumptions about how to do the basic stuff of an application, but the sophisticated framework-based design of Rails means that you can now build on this simple basic application to enhance its appearance and capabilities without losing any of what you have already done.

In the remainder of this article we're going to go back over the code above and look in more depth at what is happening, and along the way you should see some of the elegance and sophistication of the Rails framework.

On to page 2 | On to page 3 | On to page 4