简体   繁体   中英

Spree commerce taxonomies error

undefined method `each' for nil:NilClass

<% max_level = Spree::Config[:max_level_in_taxons_menu] || 1 %>
  <nav id="taxonomies" class="sidebar-item" data-hook>

  <% @taxonomies.each do |taxonomy| %>
    <% unless taxonomy.name == 'Tags' %>
      <h6 class="taxonomy-root"><%= Spree.t(:shop_by_taxonomy, :taxonomy => taxonomy.name) %></h6>
      <%= taxons_tree(taxonomy.root, @taxon, Spree::Config[:max_level_in_taxons_menu] || 1) %>

This is the error i am getting and this is the code i updated to taxonomies.each.to_a not sure if that is right and how do i update the code so the server will run the new code instead of the older code?

Here is the updated code

<% max_level = Spree::Config[:max_level_in_taxons_menu] || 1 %>
<nav id="taxonomies" class="sidebar-item" data-hook>
  <% @taxonomies.each.to_a do |taxonomy| %>
    <% cache [I18n.locale, taxonomy, max_level] do %>
      <h6 class='taxonomy-root'><%= Spree.t(:shop_by_taxonomy, :taxonomy => taxonomy.name) %></h6>
      <%= taxons_tree(taxonomy.root, @taxon, max_level) %>
    <% end %>
  <% end %>
</nav>

Looks like what you're doing is forcing a nil object to an array, which will work, yt isn't an ideal solution. You can wrap the whole thing in this:

<% if @taxonomies && @taxonomies.any? %>
  #your code goes here
<% end %>

...which will check for the presence of a taxonomies variable and make sure it isn't an empty array before running your code. As far as getting the code on the server goes, you'll have to post more info about your deployment setup and version control to answer that question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM