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 calleach_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 calleach_with_index
on it. Your cache will work if you fix the error.