Rails 3.2 undefined method `key?' for nil:NilClass
I am a newbie to RoR and got the same error while going through RoR "Getting started guide".
This may sound silly, but others might make the same mistake as I am, so posting what I noticed in RoR from newbie's eyes,
validates :name, : presence => true
Note ": presence", it should be ":presence". Now the Rail throws a Syntax error
C:/blog/app/models/post.rb:4: syntax error, unexpected ':', expecting keyword_end validates :name, : presence => true
But if you "Refresh" Your browser, it hides the syntax error, instead gives
undefined method `key?' for nil:NilClass
It seems Rails caching is the culprit. :)
This may not be your issue as it sounds like you already had working code to begin with, but for future reference for others, I ran into the same error due to a simple typo inside my model code, like so:
class Foo < ActiveRecord::Base validates :content, :length => { maximum => 10 }end
which should have been:
class Foo < ActiveRecord::Base validates :content, :length => { :maximum => 10 }end
Note the "maximum" vs. ":maximum" -- this resulted in the exact nil error above, and went away when I fixed that typo.
Not sure if this is the same bug, but I was having a similar issue. On a first load of a buggy model, Rails responds with a Routing error and then for the requests coming afterwards it responds with undefined method 'key?' for nil:NilClass
and same stack trace.
This looks to be a bug with Rails class caching, but can get around by enabling class caching or disabling on change class reloading.
config.cache_classes = true
or
config.cache_classes = falseconfig.reload_classes_only_on_change = false