【问题标题】:Spree display only selected taxons on sidebar for different pages狂欢在不同页面的侧边栏上仅显示选定的分类单元
【发布时间】:2012-08-03 21:07:17
【问题描述】:

您好,我也是 spree 和 rails 的新手。请帮帮我。

我想在页面的侧边栏上显示选定的分类单元。 假设我的分类单元结构是这样的

Dresses
   -Party
      -sub category
   -Casual
      -sub category
      -sub category
   -Formal

Pants
   -Party
   -Casual
   -Formal
Shirts
   -Party
   -Casual
   -Formal

所以当我在*www.host.com/t/pants*

我只想显示

Pants
   -Party
   -Casual
   -Formal

在侧边栏中隐藏所有其他分类树。

请帮帮我。

部分代码在这里显示所有分类法

<nav id="taxonomies" class="sidebar-item" data-hook>

  <% get_taxonomies.each do |taxonomy| %>

    <h6 class='taxonomy-root'><%= t(taxonomy.name.singularize) %></h6>


    <%= taxons_tree(taxonomy.root, @taxon, Spree::Config[:max_level_in_taxons_menu] || 1) %>

  <% end %>

</nav>

【问题讨论】:

  • 我需要您的帮助来设置相同的分类结构。

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


【解决方案1】:

当前选择的分类单元(例如 www.host.com/t/pants)可从

获得
@searcher.properties[:taxon] 

这是裤子分类对象,你可以比较它来过滤get_taxonomies结果。

所以我猜你可以这样做:

<% get_taxonomies.select{|t| @searcher.properties[:taxon].nil? or t.id == @searcher.properties[:taxon].id }.each do |taxonomy| %>

但是当您在子类别中时,这将不起作用(例如 /t/pants/party)

在早期的 Spree 版本中似乎有一个 .ancestors 方法,但现在它已经消失了,所以我想仍然有一种快速的方法来检查分类单元的祖先,但我还没有找到确切的方法。

完整的解决方案是这样的:

<% get_taxonomies.select{|t| @searcher.properties[:taxon].nil? or t.ancestors.includes( @searcher.properties[:taxon].id) }.each do |taxonomy| %>

希望对你有帮助

【讨论】:

    【解决方案2】:

    感谢@jpdoyle 的帮助。

    比较

    taxonomy.root 
    

    每个实例都有

    (@searcher.properties[:taxon]).ancestors.collect { |ancestor| seo_url(ancestor)}
    

    解决了我的问题。

    【讨论】:

      【解决方案3】:

      从 Spree 3.7 开始,原始方法如下,我们可以覆盖 @taxonomies 变量值:

      def show
        @taxon = Taxon.friendly.find(params[:id])
        return unless @taxon
      
        @searcher = build_searcher(params.merge(taxon: @taxon.id, include_images: true))
        @products = @searcher.retrieve_products
        taxonomies = Spree::Taxonomy.includes(root: :children)
        @taxonomies = taxonomies.select { |t| t.id == @taxon.taxonomy_id }
      end
      

      所以我们只能选择那些与所选分类单元实际相关的分类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-03
        • 2012-01-13
        • 1970-01-01
        • 2019-10-13
        • 1970-01-01
        • 1970-01-01
        • 2015-09-19
        • 1970-01-01
        相关资源
        最近更新 更多