2016-04-06 7 views
1

Ich arbeite an einer Rails-Anwendung mit Spree Commerce. Ich verwende sie nicht als Kategorie, sondern als eine Art von Registry-Kann nicht alle Produkte anzeigen

Ich versuche alle Produkte in einer Taxon Ursache anzuzeigen.

Also, ich bearbeite /app/views/spree/taxons/show.html.erb. Eigens diese Zeile:

<%= render partial: 'spree/shared/products', locals: { products: @products, taxon: @taxon } %> 

Ändern es für:

<%= render :partial => 'spree/shared/products', :locals => { :products => @products } %> 

Aber es sagt noch "Keine Produkte gefunden". Aber in meinem Index sind sie gezeigt.

Also, wie kann ich alle Produkte richtig zeigen?

Vielen Dank im Voraus.

ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux] 
Rails 4.2.5 
Spree 3.0.4 

Wie gewünscht:

1) Dies ist die Show von Produkten ist

<% @body_id = 'product-details' %> 

<% cache [I18n.locale, current_currency, @product, @product.possible_promotions] do %> 
    <div data-hook="product_show" itemscope itemtype="https://schema.org/Product"> 
    <div class="col-md-4" data-hook="product_left_part"> 
     <div data-hook="product_left_part_wrap"> 
     <div id="product-images" data-hook="product_images"> 
      <div id="main-image" class="panel panel-default" data-hook> 
      <div class="panel-body text-center"> 
       <%= render :partial => 'image' %> 
      </div> 
      </div> 
      <div id="thumbnails" data-hook> 
      <%= render :partial => 'thumbnails' %> 
      </div> 
     </div> 

     <div data-hook="product_properties"> 
      <%= render :partial => 'properties' %> 
     </div> 

     <div data-hook="promotions"> 
      <%= render :partial => 'promotions' %> 
     </div> 
     </div> 
    </div> 

    <div class="col-md-8" data-hook="product_right_part"> 
     <div data-hook="product_right_part_wrap"> 
     <div id="product-description" data-hook="product_description"> 
      <h1 class="product-title" itemprop="name"><%= @product.name %></h1> 

      <div class="well" itemprop="description" data-hook="description"> 
      <%= product_description(@product) rescue Spree.t(:product_has_no_description) %> 
      </div> 

      <div id="cart-form" data-hook="cart_form"> 
      <%= render :partial => 'cart_form' %> 
      </div> 
     </div> 

     <%= render :partial => 'taxons' %> 
     </div> 
    </div> 
    </div> 
<% end %> 

2) Zeigen von Taxa

<h1 class="taxon-title"><%= @taxon.name %></h1> 

<% content_for :sidebar do %> 
    <div data-hook="taxon_sidebar_navigation"> 
    <%= render partial: 'spree/shared/taxonomies' %> 
    <%= render partial: 'spree/shared/filters' if @taxon.leaf? %> 
    </div> 
<% end %> 

<div data-hook="taxon_products"> 
    <%= render partial: 'spree/shared/products', locals: { products: @products, taxon: @taxon } %> 
</div> 

<% unless params[:keywords].present? %> 
    <div data-hook="taxon_children"> 
    <% cache [I18n.locale, @taxon] do %> 
     <%= render partial: 'taxon', collection: @taxon.children %> 
    <% end %> 
    </div> 
<% end %> 

3) Dies ist, wie ich machen in meinem Index

<div data-hook="homepage_products"> 
    <% cache(cache_key_for_products) do %> 
    <%= render :partial => 'spree/shared/products', :locals => { :products => @products } %> 
    <% end %> 
</div> 

4) _products.html.erb

<% 
    paginated_products = @searcher.retrieve_products if params.key?(:keywords) 
    paginated_products ||= products 
%> 

<% content_for :head do %> 
    <% if paginated_products.respond_to?(:num_pages) %> 
    <%= rel_next_prev_link_tags paginated_products %> 
    <% end %> 
<% end %> 

<div data-hook="products_search_results_heading"> 
    <% if products.empty? %> 
    <div data-hook="products_search_results_heading_no_results_found"> 
     <%= Spree.t(:no_products_found) %> 
    </div> 
    <% elsif params.key?(:keywords) %> 
    <div data-hook="products_search_results_heading_results_found"> 
     <h6 class="search-results-title"><%= Spree.t(:search_results, keywords: h(params[:keywords])) %></h6> 
    </div> 
    <% end %> 
</div> 

<% if products.any? %> 
    <div id="products" class="row" data-hook> 
    <% products.each do |product| %> 
     <% url = spree.product_url(product, taxon_id: @taxon.try(:id)) %> 
     <div id="product_<%= product.id %>" class="col-md-3 col-sm-6 product-list-item" data-hook="products_list_item" itemscope itemtype="https://schema.org/Product"> 
     <div class="panel panel-default"> 
      <% cache(@taxon.present? ? [I18n.locale, current_currency, @taxon, product] : [I18n.locale, current_currency, product]) do %> 
      <div class="panel-body text-center product-body"> 
       <%= link_to small_image(product, itemprop: "image"), url, itemprop: 'url' %><br/> 
       <%= link_to truncate(product.name, length: 50), url, class: 'info', itemprop: "name", title: product.name %> 
      </div> 
      <div class="panel-footer text-center"> 
       <span itemprop="offers" itemscope itemtype="https://schema.org/Offer"> 
       <span class="price selling lead" itemprop="price"><%= display_price(product) %></span> 
       </span> 
      </div> 
      <% end %> 
     </div> 
     </div> 
    <% end %> 
    <% reset_cycle("classes") %> 
    </div> 
<% end %> 

<% if paginated_products.respond_to?(:num_pages) %> 
    <%= paginate paginated_products, theme: 'twitter-bootstrap-3' %> 
<% end %> 

Controller-Index:

module Spree 
    class HomeController < Spree::StoreController 
    helper 'spree/products' 
    respond_to :html 

    def index 
     @searcher = build_searcher(params.merge(include_images: true)) 
     @products = @searcher.retrieve_products 
     @taxonomies = Spree::Taxonomy.includes(root: :children) 
    end 
    end 
end 
+0

können Sie die Controller-Methode anzuzeigen beziehen d zu Ihrer Indexseite (wo die Produkte existieren)? –

+0

Nur hinzugefügt. Am Ende – Thrasher

Antwort

2

Zum spree/frontend/app/controllers/spree/taxons_controller.rb

die show Verfahren wie diese eine Antwort auf „Keine Produkte gefunden:“ Ich bin auf der Suche

def show 
     @taxon = Taxon.friendly.find(params[:id]) 
     return unless @taxon 

     @searcher = build_searcher(params.merge(include_images: true)) 
     @products = @searcher.retrieve_products 
     @taxonomies = Spree::Taxonomy.includes(root: :children) 
    end 
+0

falsche Anzahl von Argumenten (gegeben 2, erwartete 1) – Thrasher

+0

'@searcher = build_searcher (build_searcher (params.merge (include_images: true)))' – Thrasher

+0

sorry, ich aktualisierte die Antwort –

0

Zunächst sollten Sie Ihre Datei _products.html.erb aufgerufen werden.

Aber es sagt noch „Keine Produkte gefunden“

Zweitens könnten Sie uns bitte zeigen diese Datei?

Aber in meinem Index werden sie gezeigt.

Können Sie uns auch zeigen, wie Sie Ihre Produkte in Ihrem Index rendern?

EDIT: Es scheint wie in Taxons#show, die @products Instanzvariable ist nicht festgelegt. Überprüfen Sie, ob Sie es in der Show-Aktion in Ihrem TaxonsController festlegen.

+0

Ich habe gerade die Informationen hinzugefügt, die Sie gefragt haben. Vielen Dank! – Thrasher

+0

Haben Sie eine Datei namens _products.html.erb in '/ app/views/spree/shared /'? Wenn ja, könnten wir es sehen? – born4new

+0

Okay. Gerade noch einmal bearbeitet. – Thrasher

-1

Eigentlich aussehen Hier ist meine Frage Link: Spree Commerce: Nein Produkte gefunden Spree Commerce: No Products Found

Ich bin gerade bei stackoverflow.com ein paar Minuten beigetreten, kann oben keinen Kommentar hinzufügen Antworten.

Hier ist /controllers/spree/taxons_controller.rb:

module Spree 

Klasse TaxonsController < Spree :: StoreController rescue_from Active :: RecordNotFound, mit: render_404 Helfer 'spree/products'

respond_to :html 

def show 
    @taxon = Taxon.friendly.find(params[:id]) 
    return unless @taxon 

    @searcher = build_searcher(params.merge(taxon: @taxon.id, include_images: true)) 

    sorting_scope = params[:order].try(:to_sym) || :ascend_by_updated_at 

    @taxonomies = Spree::Taxonomy.includes(root: :children) 

    @searcher.retrieve_products 

    @products = @searcher.retrieve_products 



    @curr_page = params[:page] || 1 
    @products_per_page = Spree::Config.products_per_page 

    @start_no = @curr_page == 1 ? 1 : ((@curr_page.to_i-1)*@products_per_page)+1 

    @end_no = @products.length 

    if @curr_page.to_i > 1 
    @end_no = ((@curr_page.to_i-1)*@products_per_page) + @end_no 
    end 

# @end_no = @product_count <= @product_count ? @product_count : @products.length+(@curr_page.to_i-1)*@products_per_page 

end 

private 

def accurate_title 
    @taxon.try(:seo_title) || super 
end 

Ende Ende

Verwandte Themen