Ruby on Rails doesn't like me

I wrote this about 2 months ago. Seemed a shame to let it rot in my drafts, so here it is. After a little more experience with it, I kind of like Rails, but I echo the general discontent going around about it being too much magic.

So, anyone in the geek world knows that Ruby on Rails is the hot new thing nowadays. It’s everywhere, it’s hot, it’s sexy, it wins. Let me show you how awesome it is, with a quick tutorial: writing a fortune cookie application in 20 minutes.

Easy start, just follow the instructions in Agile Web Development with Rails, and just adapt the general gist of things to our fortune cookie application. I’ll skip the boring application creation and database configuration, and jump right in with the creation of the models, migrations and controllers. We are writing a fortune cookie application, so obviously we’ll need to handle fortune cookies. Let’s have a cookie model:

$ ./scripts/generate model cookie

Then edit the migration to define a few columns…

class CreateCookies < ActiveRecord::Migration
  def self.up
    create_table :cookies do |t|
      t.column :submitter, :string
      t.column :body, :text
    end
  end

  def self.down
    drop_table :cookies
  end
end

A quick run of rake db:migrate, and we have a cookies table! Now, let’s generate an admin controller, as the book does, and have it contain a dynamically generated scaffold:

$ ./scripts/generate controller admin

And just add scaffold :cookie in the controller body. Boot a WEBrick server, and we should be good to go! Just hit http://localhost:3000/admin, and…

Rails error

Rails error

Oops! That’s not supposed to happen. What the heck is this ‘Cooky’ thing it’s complaining about? The similarity with our cookie concepts is certainly visible, but we haven’t defined any ‘cooky’ anywhere?!

(ten minutes pass, google and #rubyonrails are consulted)

So, apparently, Rails is perfectly capable of pluralizing automatically ‘cookie’ into ‘cookies’, but when the scaffolding code wants to reverse engineer forms and such from the database, it singularizes ‘cookies’ into ‘cooky’. Oops.

After a crash course in using the Inflector (supposed to be a more advanced concept, but I guess we’ll have to find out about it now) to kick Rails into getting it right. In config/environment.rb:

Inflector.inflections do |inflect|
  inflect.irregular 'cookie', 'cookies'
end

Okay, now that we’ve explained to Rails how that works, can we get going? Reload that page!

Rails error, again

Rails error, again

Oh for crying out loud, what now?

(five minutes pass googling and asking for help on #rubyonrails)

Oops, time is up! We’ve had our 20 minutes, and… Well, all we’ve got to show for it is a Rails framework that, despite namespacing, manages to confuse my Cookie model class and what I assume is WEBrick’s internal HTTP cookie handling class.

Hmm, what if I try starting the app under Mongrel, instead of WEBrick?

<hackity hack, installing mongrel>
$ ./scripts/server
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/lib/ruby/1.8/http11.so: /usr/lib/ruby/1.8/http11.so: invalid ELF header - /usr/lib/ruby/1.8/http11.so (LoadError)
<traceback>

Wow, epic fail!

Two things I’ve learned so far: first, it appears that Rails doesn’t like me; second, I now know why no Rails tutorials use a fortune cookie application to demonstrate the strengths of Rails.