繁体   English   中英

Rails caches_page 没有读取实例变量并抛出错误

[英]Rails caches_page is not reading instance variables and throwing error

我正在尝试在 Rails 中实现缓存并希望缓存整个主页。 到目前为止,最快的方法是在 controller 中使用cache_page:home方法,但它似乎一次都不起作用。 这是我home方法。

  def home 
    @banners = BannerUpload.order('series').with_attached_image
    img = ActionController::Base.helpers.asset_path("share-meta.png")

    @sections = Section.includes(:homepage_items).where(active: true).order('position')
    @testimonials = Testimonial.where(status: true)
  end

这是我得到的错误:

undefined method each_with_index for nil:NilClass for @banners object,意味着即使第一次查询也没有命中。

我也找不到任何关于这个问题的文章。 我尝试设置不同的商店但没有帮助:

config.cache_store =:mem_cache_storeconfig.cache_store =:memory_store

编辑

我自己解决了。 实际上我不知道默认情况下页面缓存不包含在rails中。 所以我不得不将它包含在 gem 'actionpack-page_caching' 之后,它工作正常。

就像您说的那样:即使第一次查询也没有命中。 所以看起来@banners是 nil 并且您想在其上调用each_with_index ,这与缓存无关。 在您看来,您可以先检查@banners是否不为零,然后在其上调用each_with_index 如果您修复错误,您的缓存将起作用。

我自己解决了。 实际上我不知道默认情况下页面缓存不包含在rails中。 所以我不得不将它包含在gem 'actionpack-page_caching'之后,它工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM