簡體   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