在Rails 5中渲染和缓存异构集合

时间:2017-02-04 23:20:26

标签: ruby-on-rails ruby caching activerecord ruby-on-rails-5

我正在开发一个活动流,并为每种类型的事件设置一个包含子类的事件模型,例如StatusChanged等。

每种类型的事件都有一个相应的部分,用于格式化事件以进行渲染。

Rails可以以这种方式呈现异构集合,并且在我尝试使用cached: true选项在渲染调用上缓存集合之前,它的工作正常。

以下是我正在做的事情:

= render partial: events, as: :event, cached: true

其中events是异类集合。

我还重写了to_partial_path on事件,以便部分可以存储在events / _event_type.html.haml vs events / event_type / _event_type.html.haml中:

# Render partials from events/_<type> rather than default events/<type>/_<type>
  def to_partial_path
    self.class.name.underscore
  end

这是我遇到的问题的堆栈跟踪的顶部:

NoMethodError - undefined method `virtual_path' for nil:NilClass:
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer/collection_caching.rb:35:in `expanded_cache_key'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer/collection_caching.rb:30:in `block in collection_by_cache_keys'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer/collection_caching.rb:29:in `collection_by_cache_keys'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer/collection_caching.rb:15:in `cache_collection_render'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:326:in `block in render_collection'
  actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
  actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:319:in `render_collection'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:308:in `render'
  actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:47:in `render_partial'
  actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:21:in `render'
  actionview (5.0.0.1) lib/action_view/helpers/rendering_helper.rb:32:in `render'
  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
  haml (4.0.7) lib/haml/helpers.rb:89:in `non_haml'
  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'
  app/views/job_applications/_activity.html.haml:7:in `_app_views_job_applications__activity_html_haml__883799091596117587_70254600015120'
  actionview (5.0.0.1) lib/action_view/template.rb:158:in `block in render'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
  actionview (5.0.0.1) lib/action_view/template.rb:348:in `instrument'
  actionview (5.0.0.1) lib/action_view/template.rb:156:in `render'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:343:in `render_partial'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:311:in `block in render'
  actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
  actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:310:in `render'
  actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:47:in `render_partial'
  actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:21:in `render'
  actionview (5.0.0.1) lib/action_view/helpers/rendering_helper.rb:32:in `render'
  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
  haml (4.0.7) lib/haml/helpers.rb:89:in `non_haml'
  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'
  app/views/job_applications/_edit.html.haml:29:in `_app_views_job_applications__edit_html_haml__4043825476406280496_70254604867100'
  actionview (5.0.0.1) lib/action_view/template.rb:158:in `block in render'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
  actionview (5.0.0.1) lib/action_view/template.rb:348:in `instrument'
  actionview (5.0.0.1) lib/action_view/template.rb:156:in `render'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:343:in `render_partial'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:311:in `block in render'
  actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
  actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
  actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:310:in `render'
  actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:47:in `render_partial'
  actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:21:in `render'
  actionview (5.0.0.1) lib/action_view/rendering.rb:103:in `_render_template'
  actionpack (5.0.0.1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
  actionview (5.0.0.1) lib/action_view/rendering.rb:83:in `render_to_body'
  actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
  actionpack (5.0.0.1) lib/action_controller/metal/renderers.rb:144:in `render_to_body'
  actionpack (5.0.0.1) lib/abstract_controller/rendering.rb:26:in `render'

挖掘lib/action_view/renderer/partial_renderer/collection_caching.rb:35看起来@template似乎是nil

同样值得注意的是,虽然该集合中只有一种类型的事件,但这个问题并没有出现。一旦收集包含两种类型的事件......繁荣。

有没有人遇到过这个?它可能像“你不能以这种方式缓存异类集合”一样直截了当。

0 个答案:

没有答案