Theo Sin
Theo Sin

Reputation: 11

undefined local variable or method `cache_key_for_vendor_products'

I've installed the Spree Multi-Vendor gem and have built my Vendor Show page, but am getting a NameError message from the cache_key_for_vendor_products line. Here is my Vendor Controller

module Spree
  class VendorsController < Spree::StoreController
    before_action :load_taxon, only: :show

     def index
       @vendors = Spree::Vendor.active
     end
    def show
       @vendor = Spree::Vendor.active.friendly.find(params[:id])
       @searcher = build_searcher(searcher_params)
       @products = @searcher.retrieve_products.where(vendor_id: @vendor.id)
     end

     private

    def load_taxon
      @taxon ||= Spree::Taxon.friendly.find(params[:taxon_id]) if params[:taxon_id].present?
    end

    def searcher_params
      searcher_params = params.merge(include_images: true)
      searcher_params[:taxon] = load_taxon.id if load_taxon.present?
      searcher_params
    end

    def vendor_params
      params.require(:spree_vendor).permit(
        *Spree::PermittedAttributes.vendor_attributes,
        stock_locations_attributes: Spree::PermittedAttributes.stock_location_attributes
      )
    end

    def vendor_user_params
      params.require(:spree_vendor).require(:user).permit(Spree::PermittedAttributes.user_attributes)
    end

    def vendor_stock_location_params
      params.require(:spree_vendor).require(:stock_location).permit(Spree::PermittedAttributes.stock_location_attributes)
    end
  end
end

My Vendor Show

<% description = simple_format(@vendor.about_us, class: 'mb-0') %>
 <% short = truncate(description, separator: ' ', length: 200, escape: false) %>


 <div class="container">
   <div class="row px-lg-3 mt-3 vendor-details">
     <div class="col-12 text-uppercase text-center mt-4 mb-5 mb-sm-4">
       <h1 class="mt-2 mb-4"><%= @vendor.name %></h1>

     </div>
     <div class="col-12 col-sm-6 col-lg-5 text-center pt-sm-4 pt-lg-3 pb-sm-5 vendor-details-image">
       <% if @vendor.image.present? %>
         <%= image_tag main_app.url_for(@vendor.image.attachment), class: 'mw-100' %>
       <% end %>
     </div>
     <div class="col-12 col-sm-6 col-lg-5 offset-lg-1 mt-5 mt-sm-0 d-flex flex-column justify-content-center vendor-description">
       <div class="vendor-details-about">
         <% if description == short %>
           <%= description %>
         <% else %>
           <div class="js-readMore">
             <%= tag.div class: "js-readMore-short" do %>
               <%= short %>
             <% end %>

             <%= tag.div class: "js-readMore-full d-none" do %>
               <%= description %>
             <% end %>
           </div>
         <% end %>
       </div>
     </div>
   </div>
   <p><%= @vendor.contact_us %></p>

   <hr/>

   <div class="row">
     <div class="col-12 mt-5 vendor-details-products">
       <% cache(cache_key_for_vendor_products) do %>
        <%= render template: @taxon ? 'spree/taxons/show' : 'spree/products/index' %>
      <% end %>
     </div>
   </div>
 </div>

And Multi Vendor Helpers located in lib/spree_multi_vendor/multi_vendor_helpers.rb

module SpreeMultiVendor
   module MultiVendorHelpers
     include ::Spree::ProductsHelper
     include ::Spree::FrontendHelper

     def cache_key_for_vendor_products
       count = @products.count
       max_updated_at = (@products.maximum(:updated_at) || Date.today).to_s(:number)
       products_cache_keys = "spree/vendor-#{@vendor.slug}/products/all-#{params[:page]}-#{params[:sort_by]}-#{max_updated_at}-#{count}"
       (common_product_cache_keys + [products_cache_keys]).compact.join('/')
     end
   end
 end 

I've gotten these codes here: https://github.com/spree-contrib/spree_multi_vendor/pull/120/files

On my vendor show page, I get this error and I can't for the life of me figure out what went wrong:

NameError in Spree::Vendors#show

undefined local variable or method `cache_key_for_vendor_products' for #<#Class:0x00007fc173a88478:0x00007fc176034b40>

Can any of you help?

Upvotes: 1

Views: 504

Answers (0)

Related Questions