Rails6、@unbound_templates在actionview/lib/action_view/template/resolver.rb中为nil

Rails 6, @unbound_templates is nil in actionview/lib/action_view/template/resolver.rb

正在从Rails5.2.3升级到6.0.0或6.0.1,配合Ruby2.6.3,捆绑成功后,rails app:update命令已仔细运行,并且更新了 webpacker,对任何不需要身份验证的页面的请求都会给出:

Puma caught this error: undefined method `clear' for nil:NilClass (NoMethodError)
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/template/resolver.rb:181:in `clear_cache'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/path_set.rb:16:in `each'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/path_set.rb:16:in `each'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/lookup_context.rb:79:in `block in clear'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/lookup_context.rb:78:in `each'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/lookup_context.rb:78:in `clear'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/cache_expiry.rb:40:in `clear_cache'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/cache_expiry.rb:30:in `block (2 levels) in clear_cache_if_necessary'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/file_update_checker.rb:83:in `execute'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/cache_expiry.rb:32:in `block in clear_cache_if_necessary'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/cache_expiry.rb:23:in `synchronize'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/cache_expiry.rb:23:in `clear_cache_if_necessary'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/cache_expiry.rb:11:in `before'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:429:in `block in make_lambda'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:201:in `block (2 levels) in halting'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:607:in `block (2 levels) in default_terminator'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:606:in `catch'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:606:in `block in default_terminator'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:202:in `block in halting'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:514:in `block in invoke_before'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:514:in `each'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:514:in `invoke_before'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:134:in `run_callbacks'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/execution_wrapper.rb:111:in `run!'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/execution_wrapper.rb:73:in `block in run!'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/execution_wrapper.rb:70:in `tap'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/execution_wrapper.rb:70:in `run!'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/executor.rb:12:in `call'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/static.rb:126:in `call'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/webpacker-4.2.0/lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/rack-proxy-0.6.5/lib/rack/proxy.rb:57:in `call'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/engine.rb:526:in `call'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/railtie.rb:190:in `public_send'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/railtie.rb:190:in `method_missing'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/rack-2.0.7/lib/rack/deflater.rb:34:in `call'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/puma-4.3.0/lib/puma/configuration.rb:228:in `call'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/puma-4.3.0/lib/puma/server.rb:667:in `handle_request'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/puma-4.3.0/lib/puma/server.rb:470:in `process_client'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/puma-4.3.0/lib/puma/server.rb:328:in `block in run'
/Users/Emma/.rvm/gems/ruby-2.6.3/gems/puma-4.3.0/lib/puma/thread_pool.rb:134:in `block in spawn_thread'

对同一页面的第二个请求给出:

Error during failsafe response: undefined method `compute_if_absent' for nil:NilClass
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/template/resolver.rb:199:in `block in query'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/template/resolver.rb:196:in `map'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/template/resolver.rb:196:in `query'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/template/resolver.rb:189:in `_find_all'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/template/resolver.rb:121:in `block in find_all'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/template/resolver.rb:156:in `block in cached'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/template/resolver.rb:66:in `cache'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/template/resolver.rb:155:in `cached'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/template/resolver.rb:120:in `find_all'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/path_set.rb:77:in `block (2 levels) in _find_all'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/path_set.rb:76:in `each'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/path_set.rb:76:in `block in _find_all'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/path_set.rb:75:in `each'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/path_set.rb:75:in `_find_all'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/path_set.rb:55:in `find_all'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/path_set.rb:48:in `find'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/lookup_context.rb:129:in `find'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/renderer/template_renderer.rb:47:in `determine_template'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/renderer/template_renderer.rb:9:in `render'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/renderer/renderer.rb:29:in `render_to_object'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/rendering.rb:118:in `block in _render_template'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/base.rb:304:in `in_rendering_context'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/rendering.rb:117:in `_render_template'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/streaming.rb:219:in `_render_template'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/rendering.rb:103:in `render_to_body'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/rendering.rb:52:in `render_to_body'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/renderers.rb:142:in `render_to_body'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/abstract_controller/rendering.rb:25:in `render'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/rendering.rb:36:in `render'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
  /Users/Emma/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/core_ext/benchmark.rb:14:in `ms'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/instrumentation.rb:85:in `cleanup_view_runtime'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.1/lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/instrumentation.rb:43:in `render'
  /Users/Emma/rails/consonance/app/controllers/exceptions_controller.rb:20:in `block (2 levels) in show'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/mime_responds.rb:214:in `respond_to'
  /Users/Emma/rails/consonance/app/controllers/exceptions_controller.rb:14:in `show'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/abstract_controller/base.rb:196:in `process_action'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/rendering.rb:30:in `process_action'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/abstract_controller/callbacks.rb:42:in `block in process_action'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/callbacks.rb:135:in `run_callbacks'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/abstract_controller/callbacks.rb:41:in `process_action'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/rescue.rb:22:in `process_action'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/notifications.rb:180:in `block in instrument'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/notifications.rb:180:in `instrument'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.1/lib/active_record/railties/controller_runtime.rb:27:in `process_action'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/abstract_controller/base.rb:136:in `process'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionview-6.0.1/lib/action_view/rendering.rb:39:in `process'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal.rb:191:in `dispatch'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_controller/metal.rb:236:in `block in action'
  /Users/Emma/rails/consonance/config/application.rb:172:in `block in <class:Application>'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/show_exceptions.rb:51:in `render_exception'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/show_exceptions.rb:36:in `rescue in call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/rack/logger.rb:38:in `call_app'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/rack/logger.rb:26:in `block in call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/tagged_logging.rb:80:in `block in tagged'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/tagged_logging.rb:28:in `tagged'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/tagged_logging.rb:80:in `tagged'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/rack/logger.rb:26:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/request_store-1.4.1/lib/request_store/middleware.rb:19:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/request_id.rb:27:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/skylight-core-4.0.2/lib/skylight/core/probes/action_dispatch/request_id.rb:12:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/rack-2.0.7/lib/rack/runtime.rb:22:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/executor.rb:14:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/static.rb:126:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.1/lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/webpacker-4.2.0/lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/rack-proxy-0.6.5/lib/rack/proxy.rb:57:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/engine.rb:526:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/railtie.rb:190:in `public_send'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/railtie.rb:190:in `method_missing'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/rack-2.0.7/lib/rack/deflater.rb:34:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/puma-4.3.0/lib/puma/configuration.rb:228:in `call'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/puma-4.3.0/lib/puma/server.rb:667:in `handle_request'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/puma-4.3.0/lib/puma/server.rb:470:in `process_client'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/puma-4.3.0/lib/puma/server.rb:328:in `block in run'
  /Users/Emma/.rvm/gems/ruby-2.6.3/gems/puma-4.3.0/lib/puma/thread_pool.rb:134:in `block in spawn_thread'

这意味着@unbound_templates引入的变量herenil。在 Rails 控制台上,Concurrent::Map.new 工作正常。

这个在初始化器中设置的实例变量怎么会是 nil?

我遇到了完全相同的错误,并且到目前为止调试没有任何进展。我怀疑它是由 gem 引起的,但我还不知道。

@snowangel 如果你想比较我在这里发布的 Gemfiles:https://gist.github.com/biscuitvile/7bf0d4423ce0b2aa79e0ccc08bff4295

注意:我在第二次请求时遇到了同样的错误

undefined method `compute_if_absent' for nil:NilClass

TLDR;

仅将数据库的 prepared_statements 配置为 truefalse


解释

就我而言,我错误地将 prepared_statements 配置为 nil

ActiveRecord 上的 this line 中,它调用 @find_by_statement_cache,然后调用 compute_if_absent

def cached_find_by_statement(key, &block)
  cache = @find_by_statement_cache[connection.prepared_statements]
  # THIS LINE BELOW
  cache.compute_if_absent(key) { StatementCache.create(connection, &block) }
end

其中有以下 value:

@find_by_statement_cache = { true => Concurrent::Map.new, false => Concurrent::Map.new }

并且由于我已将其配置为 nil,它会呈现 undefined method compute_if_absent 错误。