zima
zima

Reputation: 733

Undefined method `each' for nil:NilClass in a partial

I am trying to display a collection as a partial ( on a side panel). I get an error above, have been fighting for several hours and do not understand what is the problem.

Containing view (a partial template included into application.html.haml file):

    %span.span12
      %ul.nav.nav-pills.nav-stacked{style: "color: white;"}
        = render :partial => 'categories/categories_list'

Partial /categories/_category_list.html.haml:

    %ul
      - @categories.each do |category|
        %li{style: "color: white;"}=category.name

Controller (categories_controller):

    class Macro::CategoriesController < ApplicationController

      def categories_list
        @categories= Category.all
      end
    end

I have separately tried to add "collection: @categories" as an option for render :partial - to non avail. When I replaced the instance variable with just an array in order to test the partial rendering, it worked ( the list items got rendered). Seem like the variable @categories does not get instantiated, however I have double-checked in console that a given activerecord query returns a set of objects. I am breaking my brain to see where I made a mistake.

Log:

    Started GET "/ios/macro/namespace2/another" for 127.0.0.1 at 2014-02-11 11:41:23 +0100
    Processing by Macro::Namespace2::AnotherController#index as HTML 
    Parameters: {"param1"=>"xx"}
    ....... (SQL queries for that view) 
    Rendered macro/namespace2/another_controller/_another_view.html.haml (7.0ms)
    Rendered macro/namespace2/another_controller/index.html.haml within layouts/application (29.6ms)
    Rendered layouts/_logout_link.html.haml (0.5ms)
    Rendered layouts/_top_navigation.html.haml (3.3ms)
    Rendered layouts/_search.html.haml (0.8ms)
    Rendered macro/categories/_categories_list.html.haml (0.8ms)
    Rendered macro/_navigation.html.haml (3.6ms)
    Completed 500 Internal Server Error in 49ms

    ActionView::Template::Error (undefined method `each' for nil:NilClass):
        1: %ul
        2: - @categories.each do |category|
        3:   %li{style: "color: white;"}=category
    app/views/macro/categories/_categories_list.html.haml:2:in    `_app_views_macro_categories__categories_list_html_haml__246519878332953677_70248836672780'
    app/views/macro/_navigation.html.haml:11:in `_app_views_macro__navigation_html_haml__3071184578230019020_70248793690940'
    app/views/layouts/application.html.haml:24:in `_app_views_layouts_application_html_haml__3208937943371424843_70248793607300'


    Rendered /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
    Rendered /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
    Rendered /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (10.6ms)

Routes:

        namespace "macro" do

          namespace "namespace2" do

            resources :another, only: [:index]
          end

        resources :categories
         end   
      end

Upvotes: 2

Views: 827

Answers (1)

carpamon
carpamon

Reputation: 6623

Probably the #categories_list action in Macro::CategoriesController is not being called. Check the log and see which controller and action is handling the request.

You should see something like:

'Processing by Macro::CategoriesController#categories_list as HTML'
'Parameters: { }'

Upvotes: 1

Related Questions