简体   繁体   中英

Rails caches_page is not reading instance variables and throwing error

I am trying to implement caching in rails and wants to cache whole homepage. So far the quickiest way is to use cache_page:home method in controller but it doesn't seems to work even once. This is my home method.

  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

this is the error I am getting:

undefined method each_with_index for nil:NilClass for @banners object, means query is not hitting even at first time.

I could not find any article on this issue as well. I have tried setting different stores but no help:

config.cache_store =:mem_cache_store or config.cache_store =:memory_store etc

EDIT :

I got it solved myself. Actually I am not aware of the fact that page caching is not included by default in rails. So I had to include it with gem 'actionpack-page_caching' after that it worked fine.

Like you are saying: Query is not hitting even at first time. So it looks like @banners is nil and you want to call each_with_index on it, this has nothing to do with caching. In your view you can first check if @banners is not nil and then call each_with_index on it. Your cache will work if you fix the error.

I got it solved myself. Actually I am not aware of the fact that page caching is not included by default in rails. So I had to include it with gem 'actionpack-page_caching' after that it worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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