Sunday, June 29, 2008

undefined method 'each' for nil

A common error that trips up Rails converts is the fact that you need to manually call 'render' inside your controller actions. If you don't, you'll see something like this:

/usr/local/lib/ruby/gems/1.8/gems/merb-core-0.9.4/lib/merb-core/rack/handler/mongrel.rb:85:in `process': undefined method `each' for nil:NilClass (NoMethodError)

You'll see the code around line 85 is:

if Proc === body
body.each { |part|
response.body << part

As you can see, if nothing is returned from your action, body will be nil, and the each call will fail with the above message.

There's a plugin in the works to help catch some of these issues. In the meantime, I hope this helps!

assets not displaying

A change was made recently to how Merb handles assets, and the result is that users with apps that predate this change will not have their assets displayed. The quickest fix is to delete the config/rack.rb file in your app and let the Merb defaults handle things.

issues with dependency

Some people have reported issues with using dependency to load gems into their environment. The issues are hard to track as it works for some and fails for others. If you do find yourself with gems not loading from a dependency call, you can always use require.

This issue is known and effort will be made to have it resolved by Merb 1.0. Til then, require when needed.

superclass mismatch

A 'superclass mismatch'  is ruby's way of telling you that you're trying to define/open a class who's superclass is different than what its previously recorded. An example:

irb(main):001:0> class Foo; end
=> nil
irb(main):002:0> class Bar; end
=> nil
irb(main):003:0> class Baz < Foo; end
=> nil
irb(main):004:0> class Baz < Bar; end
TypeError: superclass mismatch for class Baz
from (irb):4
from :0

What this usually means is that you're using a name thats already been used, and often you may not realize it. Earlier today someone in #merb had this exact issue when trying to define a class named Region. So, if you see this error popup, you can usually rename your class to something else to make sure the name is unique and that you're not inadvertently reopening a previously defined class.