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!

No comments: