Off the Rails ?

"Give me a place to stand and with a lever I will move the whole world."

With the right tools, even an average guy can fix his own blocked drain. You do not go into battle an elite airborne division armed with just a spear and two coconuts ! In the same light, to build this world class application, we first need the building blocks. In our case, they are Ruby, Gem and Rails, in that order.

Ruby

Mandrake normally comes with Ruby pre-installed in a RPM package. My v9.2 has ruby-1.8.0-4mdk installed. This version is no good for Rails and Gem (the Ruby equivalent of RPM, the de facto Red Hat package installation and management utility). I found this out after installing Gem, thinking I already had Ruby. Maybe I should have read the instructions first :-(. Anyway, the first thing you will have to do is download Ruby 1.8.3, build and install it.

$ wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.3.tar.gz
--19:20:05-- ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.3.tar.gz
=> `ruby-1.8.3.tar.gz.1'
Resolving ftp.ruby-lang.org... done.
Connecting to ftp.ruby-lang.org[210.163.138.100]:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done. ==> PWD ... done.
==> TYPE I ... done. ==> CWD /pub/ruby ... done.
==> PASV ... done. ==> RETR ruby-1.8.3.tar.gz ... done.
Length: 4,227,276 (unauthoritative)

100%[====================================>] 4,227,276 21.49K/s ETA 00:00

19:23:27 (21.49 KB/s) - `ruby-1.8.3.tar.gz.1' saved [4227276]
$ gzip -dc ruby-1.8.3.tar.gz | tar xvf -
$ cd ruby-1.8.3
$ ./configure
$ make
$ sudo make install

This will install ruby under /usr/local/bin and all Ruby libraries under /usr/local/lib/ruby.
RubyGem

The next thing to install is gem. According to the Ruby on Rails download website, all I need to do is to get hold of rubygems-0.8.11 or higher. Nothing simpler. So I clicked on this link and was taken to the Rubyforge repository, where I downloaded rubygems-0.8.11.tgz.

$ gzip -dc rubygems-0.8.11.tgz | tar xvf -
$ cd rubygems-0.8.11
./configure
$ make
$ sudo make install

Looking good. The next thing is Rails.
Rails
$ gem install rails --source http://gems.rubyonrails.com --include-dependencies
Attempting local installation of 'rails'
Local gem file not found: rails*.gem
Attempting remote installation of 'rails'
Updating Gem source index for: http://gems.rubyforge.org
Successfully installed rails-0.14.3
$ rails /home/david/myapp
/usr/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:3:in `require': no such file to load -- forwardable (LoadError)
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:3
from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:395
from /usr/local/bin/rails:9

Oops, what went wrong here ? rails was installed successfully, was it not ? why do I get this error when I invoke rails ? According to the instructions, all I need to do is run "rails /complete/path/to/new/railsapp" and magic and fireworks should happen. Well, after about a day scratching my head, turning on debug, looking at stack trace etc. I finally discovered the problem is to do with the last step I did. When I installed ruby as root, for some reason the files were all written out into /usr/local/lib/ruby without world read permissions

$ ls -lrt /usr/local/lib/ruby/1.8
drwxr-xr-x 2 root root 4096 Nov 28 22:26 bigdecimal/
-rw-r----- 1 root root 60 Nov 28 23:06 tkwinpkg.rb
-rw-r----- 1 root root 69 Nov 28 23:06 tkvirtevent.rb
-rw-r----- 1 root root 54 Nov 28 23:06 tktext.rb
-rw-r----- 1 root root 69 Nov 28 23:06 tkscrollbox.rb
-rw-r----- 1 root root 127659 Nov 28 23:06 tk.rb
-rw-r----- 1 root root 63 Nov 28 23:06 tkpalette.rb
-rw-r----- 1 root root 66 Nov 28 23:06 tkmngfocus.rb

OK, so you say, serves him right. It is his own fault for having crazy permissions. I agree. But look at the error trace above, would I have been able to determine that is the problem ? I am saying this because this is the general theme I find in the rest of the installation, in that the ruby stack traces most of the time do not seem to have any correlation with the actual system error that will help me zero in on the problem. Searching in Google does not help that much either because whichever way you go seems to take you to one of the few main Rails sites, with the procedures all pointing to a standard Windows installation. At least, I did discover that whenever you get an error saying 'constant_missing', then it seems to indicate the equivalent ClassNotFoundException in Java i.e. Ruby could not locate a particular resource. The class name it is complaining about should be converted into lower case with embedded underscores in order to locate the actual physical file it is looking for e.g. ActiveRecord translates to active_record.rb.

$ rails myapp
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create components
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/mocks/development
create test/mocks/test
create test/unit
create vendor
create vendor/plugins
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create public/.htaccess
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/breakpointer
create script/console
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/process/reaper
create script/process/spawner
create script/process/spinner
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log

Caramba ! My first Rails application. Well, at least, the skeleton for my first Rails application any way. Alas, I rejoice too soon, the rest of the journey is fraud with fire breathing dragons, green skin ogres, and funny shaped Trolls. Read on.

Next page A gold medal is a wonderful thing, but if you're not enough without the medal, you'll never be enough with it.