alexscjava
alexscjava

Reputation: 51

undefined method `each' for nil:NilClass in Spree

In Spree Store I have a side bar composed by Taxonomies. Example: Food (taxonomy) |-hamburguer |-french fries

I took the side bar made by taxonomies and I applied the code to build a customized bootstrap menu. And to do that I did that following:

I took the code bellow from _taxonomies.html.rb:

<% max_level = Spree::Config[:max_level_in_taxons_menu] || 1 %>
<nav id="taxonomies" class="sidebar-item" data-hook>
  <% @taxonomies.each 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>

And I did it:(putting it in a _main_bar_menu.html.rb file)

<% max_level = Spree::Config[:max_level_in_taxons_menu] || 1 %>
<nav class="columns sixteen">
<nav id="taxonomies" class="navbar navbar-default" data-hook>
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" 
            data-target="#menu-produtos-taxonomi" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
        </div>
        <div class="collapse navbar-collapse" id="menu-produtos-taxonomi">
            <ul class="nav navbar-nav">
                <li id="home-link" data-hook><%= link_to Spree.t(:home), spree.root_path %></li>    
                <% @taxonomies.each do |taxonomy| %>
                    <% cache [I18n.locale, taxonomy, max_level] do %>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" 
                            aria-haspopup="true" aria-expanded="false">
                            <%= Spree.t(:shop_by_taxonomy_only_name, :taxonomy => taxonomy.name) %> 
                            <span class="caret"></span>
                        </a>
                        <%= taxons_tree_menu(taxonomy.root, @taxon, max_level) %>
                    </li>
                    <% end %>
                <% end %>   
            </ul>
        </div>
    </div>
</nav>
</nav>

After that, every time I try to access a product page with its details(not a page listing the products of the same time), por exemple:

http://localhost:3000/products/soy-milk?taxon_id=3

I get the following error:

undefined method `each' for nil:NilClass

And it is pointing to that line in _main_bar_menu.html.rb (shown above):

<% @taxonomies.each do |taxonomy| %>

I'm a begginer in Ruby on Rails and Spree, and I don't know how to solve that. If you can help me please ... I would appreciate that.

Upvotes: 0

Views: 532

Answers (3)

sumit
sumit

Reputation: 265

<% @taxonomies.each do |taxonomy| %> Please write down this line as <% (@taxonomies || []).each do |taxonomy| %> It will never genrate error of nil

Upvotes: 1

maniacalrobot
maniacalrobot

Reputation: 2463

It looks like the @taxonomies collection isn't assigned to in the spree product show action. The code you've copied into the view needs to have the taxons assigned. Try looking and comparing the controllers for the index and show actions, as the index actions are assigning the taxons

Upvotes: 0

Oscar Valdez Esquea
Oscar Valdez Esquea

Reputation: 920

You are trying to iterate in an null collection. Where are you feeding data to the @taxonomies variable? What's that collection supposed to hold? Maybe the query you are executing its returning no values hence why the collection is null. Check your controller.

Upvotes: 0

Related Questions