Rails 3.2 undefined method `key?' for nil:NilClass

和自甴很熟 提交于 2019-11-28 08:12:29

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.

Laknath

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 = false
config.reload_classes_only_on_change = false

I agree with bmoeskau that you should check your model code for bugs. To my surprise, models apparently sometimes get loaded during the routing phase. I discovered:

  1. The code that causes the problem, in my case, is in the model, not in the controller where I would expect it.
  2. The undefined method `key?' for nil:NilClass error often only occurs after the first time I access the page.

As far as I can tell, what happens is

  1. Suppose we have a scaffold for Examples, with some bad code in app/models/example.rb
  2. A request is made for /examples
  3. The routing part of Rails matches that to app/controllers/examples_controller.rb but it first loads app/models/example.rb. I don't know why it loads the model, but the effect is, I conjecture: The error in the model stops short a part of the routing code, corrupting its construction of a cache of routes.
  4. At this point, if I'm lucky the error will be reported back to me in the browser. Sometimes, however, I simply get a message saying No route matches [GET] "/examples" (To be fair to Rails, this added complication seems to be the fault of not using resources :examples to make the route. The following happens regardless).
  5. A second request is made for /examples
  6. Conjecture: This time the Rails routing code tries to use its cached reference to app/controllers/examples_controller.rb but the routes cache is corrupt (a variable is nil) because the code that sets never finished running.

This last item is most annoying because the problem that caused it (the bad code in the model) isn't even being run.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!