这是我第一次问问题,所以请放轻松我:-p
我正在按照http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials(第 3.4.5 节渲染集合)上的示例使用部分渲染集合。代码看起来很简单,但显然我遗漏了一些东西。
models/expert.rb 包含以下行:
attr_accessible :name
Experts_controller.rb 在 index 方法中包含以下行:
@experts = Expert.all
views/experts/index.html.erb 包含以下行:
<%= render :partial => "expert", :collection => @experts %>
意见/专家/_expert.html.erb 包含:
<%= expert.name %>
在浏览器中查看索引页面时,出现以下错误:
NoMethodError in Experts#index
undefined method `name' for nil:NilClass
我已经为此工作了一个小时,完全被难住了:-/我错过了什么小东西?
---澄清---
在 index.html.erb 中运行 '<%= debug @experts %>' 会产生以下输出:
- !ruby/object:Expert
attributes:
id: 1
name: Bob Smith
slug: bob-smith
created_at: '2012-03-11 18:37:11.791118'
updated_at: '2012-03-11 18:55:58.179629'
changed_attributes: {}
previously_changed: {}
attributes_cache: {}
marked_for_destruction: false
destroyed: false
readonly: false
new_record: false
- !ruby/object:Expert
attributes:
id: 2
name: Steve Kamp
slug: steve-kamp
created_at: !!null
updated_at: !!null
changed_attributes: {}
previously_changed: {}
attributes_cache: {}
marked_for_destruction: false
destroyed: false
readonly: false
new_record: false