首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能显示所有产品

不能显示所有产品
EN

Stack Overflow用户
提问于 2016-04-06 15:05:44
回答 3查看 614关注 0票数 1

我正在使用Spree Commerce开发一个Rails应用程序。

我试图将所有产品显示在分类法中,因为我使用它们不是作为类别,而是作为某种注册表。

因此,我正在编辑/app/view/spree/taxons/how.html.erb。特别是这条线:

代码语言:javascript
复制
<%= render partial: 'spree/shared/products', locals: { products: @products, taxon: @taxon } %>

改为:

代码语言:javascript
复制
<%= render :partial => 'spree/shared/products', :locals => { :products => @products } %>

但它仍然写着“没有发现任何产品”。但在我的索引中显示了它们。

那么,我如何正确地展示所有的产品呢?

提前谢谢。

代码语言:javascript
复制
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
Rails 4.2.5
Spree 3.0.4

应要求:

1)这是来自产品的展览

代码语言:javascript
复制
<% @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)从分类群中显示

代码语言:javascript
复制
<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)这就是我在索引中呈现的方式

代码语言:javascript
复制
<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

代码语言:javascript
复制
<%
  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 %>

控制器索引:

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-06 15:31:09

转到spree/frontend/app/controllers/spree/taxons_controller.rb

使show方法看起来如下

代码语言:javascript
复制
    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
票数 2
EN

Stack Overflow用户

发布于 2016-04-06 15:12:17

首先,您的文件应该被称为_products.html.erb

但它仍然写着“没有发现任何产品”

第二,你能给我们看看这份文件吗?

但在我的索引中显示了它们。

你还能告诉我们你如何在你的索引中渲染你的产品吗?

编辑:在Taxons#show中,似乎没有设置@products实例变量。检查您是否在TaxonsController中的show中设置了它。

票数 0
EN

Stack Overflow用户

发布于 2017-09-03 23:29:15

实际上,我正在寻找“找不到产品”的答案,这是我的问题链接:疯狂商业:没有产品找到Spree Commerce: No Products Found

我刚加入stackoverflow.com几分钟,不能给上面的答案加上评论。

这里是/controllers/spree/taxons_控制员. is:

代码语言:javascript
复制
module Spree

类TaxonsController < Spree::StoreController rescue_from ActiveRecord::RecordNotFound,with::render_404助手‘spree/:render_404’

代码语言:javascript
复制
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

端端

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36455095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档