nil的未定义方法`each':部分中的NilClass

时间:2014-02-10 17:51:39

标签: ruby-on-rails view collections ruby-on-rails-4 partials

我正在尝试将集合显示为部分(在侧面板上)。我上面得到一个错误,已经打了好几个小时而且不明白是什么问题。

包含视图(application.html.haml文件中包含的部分模板):

    %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

我已经单独尝试添加“collection:@categories”作为渲染选项:部分 - 无效。当我用一个数组替换实例变量以测试部分渲染时,它工作(列表项被渲染)。好像变量@categories没有实例化,但我在控制台中仔细检查了给定的activerecord查询返回一组对象。我正在打破我的大脑,看看我犯了什么错误。

日志:

    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)

路线:

        namespace "macro" do

          namespace "namespace2" do

            resources :another, only: [:index]
          end

        resources :categories
         end   
      end

1 个答案:

答案 0 :(得分:1)

可能没有调用Macro :: CategoriesController中的#categories_list动作。检查日志,查看处理请求的控制器和操作。

您应该看到类似的内容:

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