【问题标题】:Undefined method `each' for nil:NilClass in a partialnil:NilClass 在部分中的未定义方法“each”
【发布时间】:2014-03-08 05:04:32
【问题描述】:

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

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

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

部分/categories/_category_list.html.haml:

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

控制器(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

【问题讨论】:

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


    【解决方案1】:

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

    您应该会看到如下内容:

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

    【讨论】:

    • 是的,看起来像这样:我看到调用包含视图的 GET 请求正在由相应的控制器处理。当它尝试渲染部分时,我什至没有看到任何请求:Rendered macro/_navigation.html.haml (1.5ms) Completed 500 Internal Server Error in 49ms ActionView::Template::Error (undefined method each' for nil:NilClass):` 是否有可能发现为什么这个部分(_navigation)中对应于render :partial 的请求没有由 categories_controller 处理?
    • 看到控制器动作没有被调用,我试图把`render partial: 'categories_list' 放在CategoriesController 中,也无济于事。还是不明白,断线在哪里?
    • 使用您的 routes.rb 文件(仅相关行)更新您的帖子并包含请求日志
    • 我已经用日志和路由更新了我的问题。我不确定这是路由错误,因为当我尝试更改部分路径时遇到路由错误(找不到控制器:类别)。在这种情况下,我没有收到这样的错误。
    • 将方法#categories_list 重命名为#index。正如我在原始答案中所说,没有调用方法 categories_list ,而是调用 index 。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    相关资源
    最近更新 更多