【问题标题】:Exclude record from rails loop - Spree从 Rails 循环中排除记录 - Spree
【发布时间】:2014-01-07 08:43:44
【问题描述】:

我正在使用related products gem 并对其稍作修改,但我有 1 个我无法弄清楚的错误。

get 方法基本上使用 searcher 类来查找所有分类并显示同一类别中的 6 个产品,在我看来,我只显示前 1 个类别,因为某些产品有多个类别。

我的问题是,在迁移中,我创建了一个名为“标签”的分类和一个名为“精选”的子分类(显示在主页上的产品),然后销毁父分类,因此它不会出现在客户身上面对网站。对于标记有特色分类单元的所有产品,都会为“is_product_line”错误抛出一个未定义的方法。所有其他未标记为特色分类单元的产品都可以完美运行。

当 get 方法在控制器中构建类别时,我需要一种方法来排除此分类单元。我已将其添加到方法中,但不确定如何将其从 @categores 中排除

控制器

module Spree
  class RelatedProductsController < Spree::StoreController
  @@display_related_items = 6

  def get
    product = Product.order("name ASC").find_by_permalink!(params[:permalink])
    ### Featured is taxonmony without a parent taxon
    featured = Spree::Taxon.where(:name => 'Featured')
    @categories = []
    product.taxons.each do |taxon|
      if taxon.taxonomy.is_product_line
        @categories << {
          :taxon => taxon,
          :searcher => build_searcher({ :taxon => taxon.id, :per_page => @@display_related_items })
        }
      end
    end
    render layout: false
    end
  end
end

获取.html.erb

<% @categories.first(1).each do |category| %>
  <%= link_to category[:taxon].pretty_name, spree.nested_taxons_path(category[:taxon].permalink) %>
  <%= render :partial => 'products', :locals => { :products => category[:searcher].retrieve_products, :taxon => category[:taxon] } %>
<% end %>

_product.html.erb

<% if products.any? %>
  <% products.each do |product| %>
  <div id="product_<%= product.id %>" class="columns large-2" data-hook="products_list_item" itemscope itemtype="http://schema.org/Product">
    <div class="product-image">
      <%= link_to product_image(product, :itemprop => "image"), product, :itemprop => 'url' %>
    </div>
    <%= link_to truncate(product.name, :length => 50), product, :class => 'info', :itemprop => "name", :title => product.name %>
    <p class="price selling" itemprop="price"><%= display_price(product) %></p>
  </div>
  <% end %>
<% end %>

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 spree


    【解决方案1】:

    我认为您应该能够从 product.taxons 中拒绝您的特色分类单元,例如

    product.taxons.reject{|t| t.name == 'Featured'}.each do |taxon|
    

    【讨论】:

      猜你喜欢
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多