First Call to a Controller, Constant is defined, Second call, “uninitialized constant Oauth”?

怎甘沉沦 提交于 2019-12-05 16:42:42

问题


I am trying to get the OAuth gem to work with Rails 3 and I'm running into this weird problem... (independent of the gem, I think I've run into this once before)

I have a controller called "OauthTestController", and a model called "ConsumerToken". The model looks like this.

require 'oauth/models/consumers/token'
class ConsumerToken < ActiveRecord::Base
  include Oauth::Models::Consumers::Token
end

When I go to "/oauth_test/twitter", it loads the Oauth::Models::Consumers::Token module and I'm able to connect to twitter no problem. But the second time I try it (just refresh the /oauth_test/twitter url), it gives me this error:

NameError (uninitialized constant Oauth):
  app/models/consumer_token.rb:4
  app/models/twitter_token.rb:2
  app/controllers/oauth_test_controller.rb:66:in `load_consumer'

Why is that? It has something to do with load paths or being in development mode maybe?


回答1:


Try using require_or_load instead of require. That forces full load each time when in development and can sometimes help with this sort of issue.




回答2:


Yeah it's something to do with being in development mode. Setting config.cache_classes = true in your development.rb get's it working (but is a pain in the ass)



来源:https://stackoverflow.com/questions/2796465/first-call-to-a-controller-constant-is-defined-second-call-uninitialized-con

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