【发布时间】: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