Ruby On Rails is a web-application framework to really productively produce web-applications in the Ruby programming language. Because of the many positive things I heard about it, I thought it may be a good idea to dedicate a couple of hours to figure out what the buzz is about. I haven’t coded a line of Ruby in my life, although I tried to read a Ruby tutorial a couple of months ago. I do have experience with other scripting languages that Ruby derived many features from, such as Perl and Python. Let’s see how far I can get in a couple of hours.
The first step, obviously, is to get all the necessary software installed. I’m doing all this on Windows, so I first download the Windows Ruby Installer. This package already comes with Ruby Gems, which apparently is a package tool that you should use to install Ruby On Rails. It also comes with a lot of other tools, such as an IDE called FreeRide, written in Ruby by the looks of it.
After launching a command prompt and typing:
gem install rails
Rails and its required components are automatically downloaded and installed. Do note that the updating of the sources list takes a very long time, using 100% of the CPU non-stop. I thought it hung, but it didn’t, as after a while it continued and asked me for each of the components whether I wanted to install it (a “Yes to all” would have been nice). “Fast” isn’t Gems’ middle name, but it works and that’s what counts. After the installation is finished I choose to do a tutorial from the Ruby on Rails website: How to make a todo list program with Rails. Highly recommended if you never used Ruby on Rails before (like me).My First Application
After creating a very simple database (one table) in phpMyAdmin:
and running a couple of scripts, editting a config file and adding two lines to a ruby file, I end up with my first running web application:
Not looking too interesting eh? Well, see that “New todo” link at the bottom? Let’s click that.
Apparently, RoR created all the basic screens and logic for us. An add item screen, modify item screen, view item screen and actions to delete items:
This feature is called scaffolding. It generates the forms and actions at run-time, simply by putting one line in your controller code:
The problem with this is that it’s not very customizable. If I would want to change the lay-out, change the form items, I got a problem. Luckily it is also possible to generate all the scaffolding code for you in your application. So that you can modify it by hand. What’s nice about the scaffold command is that you can keep it in your code and replace the different actions that it offers (such as list, edit, new etc.) one by one.
Let’s replace the default list action. We can do this simply by adding this definition to the controller code:
def list @items = Todo.find_all end
One of the scripts I ran earlier generated the code for an entity called Todo, which is automatically associated with the todos table (the table name is the plural of the entity name). By doing this, RoR is able to do a lot of things with this Todo entity. Like, as in this example, find all “instances” (i.e. table rows).Once you start replacing the default actions you can also create your own views (templates). Views are just plain HTML with special ERb tags. Which look a lot like the ASP and JSP ones. Basically they’re just short pieces of nested Ruby code:
- < %= @item.description %>
The @items variable here is the same one that I filled in the controller with all todo items. In the piece of code you can just use the description attribute of an @item. It will figure out itself that this should be bound to the description column in the todos table.RoR also offers a lot of useful commands that you can use within the view templates. Ones to generate checkboxes, links to other actions with certain parameters, for example. By using those two we can turn our previous template into:
- < %= check_box(“item”, “done”) %> < %= @item.description %> < %= link_to(“Edit”, :action => “edit”, :id => @item.id) %>
Which looks like this in the browser:
After I finish the tutorial and not having coded a lot of lines (about 50 in Ruby, many of which are empty lines and comments), I end up with a very nice todo list:
I love Ruby on Rails. It’s very easy to use and seems very productive. I’ve often complained about the amount of work that you have to put into applications that basically don’t do much more than adding, editting, linking and removing entities. RoR makes this very easy. It also surprised me that, at least for this simple example, you don’t have to know a whole lot about the Ruby language to use RoR. Being not much of a Ruby fan myself, the code I’ve written in this tutorial was pretty clean looking too.
What I also liked is how easy it was to get started. On Windows all I needed was to download the Ruby installer and MySQL and I was ready to go. It was very easy to install RoR by using gems and as RoR came with the Webrick webserver, there was no confusing Apache configuration or anything.
But as with any non-PHP web-application system there’s one big problem: hosting. Most individuals, amateur programmers, hobbyists, or whatever you like to call them, are on shared hosting. Practically none offer a Ruby installation, let alone Ruby on Rails; except for TextDrive, which looks like a viable option. I’m afraid this will be a deal-breaker for many people. Nonetheless, if RoR would be as common as PHP, I would know what to use.