【问题标题】:Sorting taxons (Brands) alphabetically按字母顺序对分类单元(品牌)进行排序
【发布时间】:2012-01-12 20:58:03
【问题描述】:

Rails 菜鸟在这里尝试在客户的 Spree 商店中调整一些东西。

侧边栏需要包含产品品牌列表,我将品牌作为分类。

我的shared/_taxonomies.html.erb 视图包含:

  <% get_taxonomies.each do |taxonomy| %>
    <% if taxonomy.name == 'Brand' %>
      <h3 class='taxonomy-root'><%= t(:shop_by_taxonomy, :taxonomy => taxonomy.name.singularize) %></h3>
      <%= taxons_tree(taxonomy.root, @taxon, Spree::Config[:max_level_in_taxons_menu] || 1) %>
    <% end %>
  <% end %>

我添加了if taxonomy.name == 'Brand' 代码以摆脱类别。 (我希望有更清洁的方法?)

如何按字母顺序列出分类单元(品牌)?

大礼包 0.70.3.

【问题讨论】:

    标签: ruby-on-rails spree


    【解决方案1】:

    设置会好很多

    @brand_taxonomy = Taxonomy.where(:name => 'Brand').first
    

    在一个通用控制器中,如果分类法显示在大多数/所有页面上,很可能是application_controller.rb,然后就去:

    <h3 class='taxonomy-root'><%= t(:shop_by_taxonomy, :taxonomy => @brand_taxonomy.name.singularize) %></h3>
    <%= taxons_tree(@brand_taxonomy.root, @taxon, Spree::Config[:max_level_in_taxons_menu] || 1) %>
    

    从而完全消除循环和条件。

    不幸的是,taxons_tree 助手直接调用了顶级分类的孩子,所以为了让孩子按名字排序,你必须重写助手,比如application_helpers.rb 为:

    def my_taxons_tree(root_taxon, current_taxon, max_level = 1)
      return '' if max_level < 1 || root_taxon.children.empty?
      content_tag :ul, :class => 'taxons-list' do
        root_taxon.children.except(:order).order(:name).map do |taxon|
          css_class = (current_taxon && current_taxon.self_and_ancestors.include?(taxon)) ? 'current' : nil
          content_tag :li, :class => css_class do
           link_to(taxon.name, seo_url(taxon)) + 
           taxons_tree(taxon, current_taxon, max_level - 1) 
          end
        end.join("\n").html_safe
      end
    end
    

    关键变化是将.except(:order).order(:name) 添加到助手的子项检索中。

    最终的视图代码如下所示:

    <h3 class='taxonomy-root'><%= t(:shop_by_taxonomy, :taxonomy => @brand_taxonomy.name.singularize) %></h3>
    <%= my_taxons_tree(@brand_taxonomy.root, @taxon, Spree::Config[:max_level_in_taxons_menu] || 1) %>
    

    application_controller.rb 中添加:

    before_filter :set_brand_taxonomy
    
    def set_brand_taxonomy
      @brand_taxonomy = Taxonomy.where(:name => 'Brand').first
    end
    

    我自己没有在 Spree 项目中实现这个,这取决于你使用 Rails 3.0.3+ 版本,但这是我建议的基本方法。

    【讨论】:

    • 感谢您提供非常详细的回答。它似乎并不像现在这样工作。由于有关未定义变量的错误,我已将@ 符号添加到my_taxons_tree(brand_taxonomy.root。但是,然后我得到undefined method root' for nil:NilClass` 这条线。我的 application_controller.rb:pastie.org/3178498(我删除了视图中的 h3 行,因为这也给出了错误。)
    • 更正缺少'@';对于那个很抱歉。所以如果你去:rails console 然后输入:Taxonomy.where(:name =&gt; 'Brand') ...它会返回什么?听起来好像没有名为“品牌”的父分类法......?
    • 另外,您将my_taxons_tree 辅助方法添加到application_controller.rb;我建议您将其放入application_helper.rb,而不是app/helpers。如果您确定要将其放入 application_controller,则需要将 helper_method :my_taxons_tree 添加到控制器顶部,并将其放入私有块中(只需在 my_taxons_tree 定义之前放置一行 private . 不过把它放在app/helpers/application_helper.rb 里更像是Rails。
    • 更正了我的展示位置(我误读了)。 application_controller.rb:pastie.org/3183894application_helper.rb:pastie.org/3183910_taxonomies.html.erb:pastie.org/3183917 Rails 控制台输出:pastie.org/3183919 错误仍然存​​在:undefined method 'name' for nil:NilClass
    • 你能把完整的堆栈跟踪放在一个馅饼里吗?这与您提到的第一个错误不同。
    猜你喜欢
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 2017-05-22
    • 1970-01-01
    相关资源
    最近更新 更多