nil的未定义方法`cache':升级到rails 4.2.5.1后的NilClass

时间:2016-01-26 19:28:53

标签: ruby-on-rails ruby ruby-on-rails-4.2

从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 个答案:

答案 0 :(得分:26)

这是一个rspec / rspec-rails issue

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

bundle update rspec-rails rspec

应该解决问题。