【发布时间】:2021-03-09 21:20:03
【问题描述】:
我正在尝试在 Rails 中实现缓存并希望缓存整个主页。到目前为止,最快的方法是在控制器中使用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 代表@banners 对象,表示即使第一次查询也没有命中。
我也找不到任何关于这个问题的文章。我尝试设置不同的商店但没有帮助:
config.cache_store = :mem_cache_store
要么
config.cache_store = :memory_store等
编辑:
我自己解决了。实际上我不知道默认情况下页面缓存不包含在rails中。所以我不得不将它包含在 gem 'actionpack-page_caching' 中,之后它工作正常。
【问题讨论】:
-
我看不出缓存如何导致 nil 错误 - 您确定您的页面在没有缓存的情况下可以正常工作吗?
标签: ruby-on-rails caching page-caching