升级到 Rails 4.2.5.1 后 nil:NilClass 的未定义方法“缓存”

Posted

技术标签:

【中文标题】升级到 Rails 4.2.5.1 后 nil:NilClass 的未定义方法“缓存”【英文标题】:undefined method `cache' for nil:NilClass after upgrading to rails 4.2.5.1 【发布时间】:2016-05-03 12:17:58 【问题描述】:

在从 rails 4.2.5 升级到最近发布的 4.2.5.1 时,我遇到了这个错误。此错误仅存在于带有before_filter 且呈现无效状态的规范,例如render file: "public/422", status: :unauthorized。 ruby-2.2.2 和 ruby​​-2.3.0 都出现此错误

action_view 解析器方法中存在错误,暗示@cache 值为nil。但是,在initialize 方法中,它应该被实例化:@cache = Cache.new

我仍在研究可重现的样本,但我至今不知道@cache 怎么可能为零。

这些是在我的test.rb 环境中设置的配置变量

config.cache_classes = true
config.eager_load = false

config.serve_static_files   = true
config.static_cache_control = 'public, max-age=3600'

config.consider_all_requests_local       = true
config.action_controller.perform_caching = false

config.action_dispatch.show_exceptions = false
config.action_controller.allow_forgery_protection = false  

config.action_mailer.default_url_options = 
  :host => '127.0.0.1',
  :port => 3000


config.action_mailer.delivery_method = :test
config.active_support.test_order = :random
config.active_support.deprecation = :stderr

堆栈跟踪:

1) UserController validates user
 Failure/Error: put :update,  id: user.id  
 NoMethodError:
   undefined method `cache' for nil:NilClass
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/template/resolver.rb:151:in `cached'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/template/resolver.rb:121:in `find_all_anywhere'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/path_set.rb:68:in `block (2 levels) in _find_all'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/path_set.rb:66:in `each'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/path_set.rb:66:in `block in _find_all'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/path_set.rb:65:in `each'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/path_set.rb:65:in `_find_all'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/path_set.rb:50:in `find_file'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/lookup_context.rb:126:in `find_file'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/renderer/abstract_renderer.rb:18:in `find_file'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/renderer/template_renderer.rb:32:in `block in determine_template'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/lookup_context.rb:147:in `with_fallbacks'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/renderer/abstract_renderer.rb:18:in `with_fallbacks'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/renderer/template_renderer.rb:32:in `determine_template'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/renderer/template_renderer.rb:8:in `render'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/renderer/renderer.rb:42:in `render_template'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/renderer/renderer.rb:23:in `render'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/rendering.rb:100:in `_render_template'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5.1/lib/action_controller/metal/streaming.rb:217:in `_render_template'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionview-4.2.5.1/lib/action_view/rendering.rb:83:in `render_to_body'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5.1/lib/action_controller/metal/rendering.rb:32:in `render_to_body'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5.1/lib/action_controller/metal/renderers.rb:37:in `render_to_body'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5.1/lib/abstract_controller/rendering.rb:25:in `render'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5.1/lib/action_controller/metal/rendering.rb:16:in `render'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5.1/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
 # ~/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5.1/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
 # ~/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
 # ~/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5.1/lib/active_support/core_ext/benchmark.rb:12:in `ms'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5.1/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5.1/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
 # ~/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5.1/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
 # ~/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5.1/lib/action_controller/metal/instrumentation.rb:43:in `render'

【问题讨论】:

【参考方案1】:

这是一个 rspec/rspec-rails issue。

您可以升级到 rspec-rails-3.4.1 来修复它。除非你在 Gemfile 中硬编码了 rspec-rails 的版本,否则运行:

bundle update rspec-rails rspec

应该解决问题。

【讨论】:

感谢修复!

以上是关于升级到 Rails 4.2.5.1 后 nil:NilClass 的未定义方法“缓存”的主要内容,如果未能解决你的问题,请参考以下文章

#=升级到Rails 5后无法正常工作

升级到 Rails 6.1 后的 Rails 路线问题

升级到 Chrome 73 后,SSL 证书在本地 Rails 上不起作用

将 Bundler 升级到 2.1.4 后初始化 Rails 控制台出错

Rake doc:app 在升级到 Ruby 2.1.1 和 Rails 4.1 后失败

升级到 OS Sierra 后 Rails 出现分段错误,可能与 sqlite3 gem 有关