未初始化的常量 ActionController::Dispatcher::MiddlewareStack
我安装了一些新的 gem 进行测试,并遇到了未初始化的常量 ActionController::Dispatcher::MiddlewareStack 错误。 我按照 gem rdocs 上的说明进行操作,在我的environment.rb 文件中指定了 gem 依赖项,然后运行 rake gems:install 和 rake gems:unpack。
我还将我的environment.rb 文件一次一行复制到另一个项目中,但没有收到错误。 我想知道还有什么其他事情可能出错。
这是错误的较长版本(我第一次运行简单测试时遇到错误;当我确实需要“active_support”时,控制台中也会发生同样的情况):
$ ruby test/unit/user_test.rb
** has_many_polymorphs: rails environment detected
/Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:105:in `const_missing': uninitialized constant ActionController::Dispatcher::MiddlewareStack (NameError)
from /Users/thaiyoshi/.gem/ruby/1.8/gems/actionpack-2.3.2/lib/action_controller/dispatcher.rb:71
from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in `require'
from /Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:521:in `new_constants_in'
from /Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in `require'
from /Users/thaiyoshi/.gem/ruby/1.8/gems/has_many_polymorphs-2.13/lib/has_many_polymorphs/autoload.rb:2
from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
... 8 levels...
from ./test/unit/../test_helper.rb:2:in `require'
from ./test/unit/../test_helper.rb:2
from test/unit/user_test.rb:1:in `require'
from test/unit/user_test.rb:1
这是我的environment.rb文件(已删除默认注释):
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
# config.gem 'has_many_polymorphs', :version => "2.12"
# require ‘has_many_polymorphs‘
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
require 'has_many_polymorphs'
require 'active_support'
Rails::Initializer.run do |config|
config.time_zone = 'UTC'
config.gem "thoughtbot-factory_girl",
:lib => "factory_girl",
:source => "http://gems.github.com"
config.gem 'thoughtbot-shoulda',
:lib => false,
:source => 'http://gems.github.com'
end
I installed some new gems for testing and ran into an uninitialized constant ActionController::Dispatcher::MiddlewareStack error. I followed the instructions on the gem rdocs-specified the gem dependency in my environment.rb file and then ran rake gems:install and rake gems:unpack.
I also copied over my environment.rb file one line at a time into a different project and I did not get the error. I was wondering what other things could be going wrong.
Here is a longer version of the error (I first ran into the error running a simple test; same thing happens in a console when I do require 'active_support'):
$ ruby test/unit/user_test.rb
** has_many_polymorphs: rails environment detected
/Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:105:in `const_missing': uninitialized constant ActionController::Dispatcher::MiddlewareStack (NameError)
from /Users/thaiyoshi/.gem/ruby/1.8/gems/actionpack-2.3.2/lib/action_controller/dispatcher.rb:71
from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in `require'
from /Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:521:in `new_constants_in'
from /Users/thaiyoshi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in `require'
from /Users/thaiyoshi/.gem/ruby/1.8/gems/has_many_polymorphs-2.13/lib/has_many_polymorphs/autoload.rb:2
from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
... 8 levels...
from ./test/unit/../test_helper.rb:2:in `require'
from ./test/unit/../test_helper.rb:2
from test/unit/user_test.rb:1:in `require'
from test/unit/user_test.rb:1
Here's my environment.rb file (default comments removed):
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
# config.gem 'has_many_polymorphs', :version => "2.12"
# require ‘has_many_polymorphs‘
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
require 'has_many_polymorphs'
require 'active_support'
Rails::Initializer.run do |config|
config.time_zone = 'UTC'
config.gem "thoughtbot-factory_girl",
:lib => "factory_girl",
:source => "http://gems.github.com"
config.gem 'thoughtbot-shoulda',
:lib => false,
:source => 'http://gems.github.com'
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我之前写了一些误导性的信息,但是,现在我已经设法坐下来不受干扰地查看它几分钟,我通过编辑环境.rb 并移动 require 'has_many_polymorphs'之后声明。
答对了。 我捂脸用力,但这就是我们学习的方式..
I previously wrote some misguided information, however, now I've managed to sit down and look at it undisturbed for a few minutes, I fixed my problem by editing my environment.rb, and moving the require 'has_many_polymorphs' statement to after the initializer block.
Bingo. I face-palmed hard, but this is how we learn..
关注这个线程已经有一段时间了 - 上面的 ric 的答案对我们的开发工作很有用,但是切换到生产模式这会导致错误。 将 require 放回初始化块上方并使用 Ric 的原始修复效果很好。 他的编辑已删除它 - 所以对于后面的任何人来说,这里又是:顺便说一句 - 我们现在在 hmp 上使用 trunk 而不是 johnsbrn 分支,这似乎与 2.3.2 现在
编辑工作得很好:它几乎解决了这个问题我。 后来我发现迁移也遇到了类似的问题。 查看代码后,我很快就找到了答案,其中涉及需要缺少的类(action_controller/middleware_stack)。
快速& 肮脏的修复是编辑相对路径下的文件:
“johnsbrn-has_many_polymorphs/lib/has_many_polymorphs/autoload.rb”
并在第一两个 require 行之间插入以下行:
require 'action_controller/middleware_stack' 除非定义? ::ActionController::MiddlewareStack
例如 它最终看起来像这样:
需要“初始化器”除非定义? ::Rails::Initializer
需要“action_controller/middleware_stack”,除非定义? ::ActionController::MiddlewareStack
需要“action_controller/dispatcher”,除非定义? ::ActionController::Dispatcher
现在一切正常。
Been following this thread for a while - ric's answer above worked for us in development but switching to production mode this caused an error. Putting the require back above the initializer block and using Ric's original fix worked fine. His edit has removed it - so for anyone following behind, here it is again: BTW - we are now using trunk on hmp rather than the johnsbrn branch and that seems to work fine with 2.3.2 now
edit: It almost fixed the issue for me. I found afterwards that migrations broke with a similar issue. After looking through code, I quickly hacked up an answer, which involved requiring the missing class (action_controller/middleware_stack).
The quick & filthy fix is to edit the file under the relative path:
"johnsbrn-has_many_polymorphs/lib/has_many_polymorphs/autoload.rb"
and insert the following line between the 1st two require lines:
require 'action_controller/middleware_stack' unless defined? ::ActionController::MiddlewareStack
e.g. It ends up looking like this:
require 'initializer' unless defined? ::Rails::Initializer
require 'action_controller/middleware_stack' unless defined? ::ActionController::MiddlewareStack
require 'action_controller/dispatcher' unless defined? ::ActionController::Dispatcher
and now things work ok.
在environment.rb中:
对我有用
in environment.rb:
works for me
如果其他人像我一样浏览到此页面,我只需将 has_many_polymorphs gem 从 1.12.1 升级到 1.13 并将其重新冻结到我的供应商文件夹中(并更新environment.rb中的 config.gem 行以使用新版本号)。
In case anyone else browses to this page like i did, i solved this simply by upgrading my has_many_polymorphs gem from 1.12.1 to 1.13 and re-freezing it into my vendor folder (and updating the config.gem line in environment.rb to use the new version number).