Showing app/views/posts/_post.html.erb where line #4 raised:
undefined method `name' for nil:NilClass
Extracted source (around line #4):
1: <p>
2: <b>Post Content:</b>
3: <%=h post.content %> by
4: <%=h post.author.name %>
5: </p>
这里是我的职位型号:为无未定义的方法:NilClass但对象存在于控制台
class Post < ActiveRecord::Base
belongs_to :board
belongs_to :author, :class_name => "User"
end
奇怪的是,如果我注释掉post.author.name,它的工作原理。而且.... 我尝试在控制台,它工作正常:
>> post
=> #<Post id: 1, content: "trying", user_id: 2, created_at: "2010-06-22 04:24:53", updated_at: "2010-06-22 04:24:53">
>> post.author
=> #<User id: 2, login: "[email protected]", name: "test1",....
>> post.author.name
=> "test1"
事实上,如果我改变post.author.name到post.user_id,它会显示正确的ID(即2) ....
什么问题?
非常感谢。
您能否显示呈现_post.html的控制器操作方法?以及呈现它的视图? – Pablo 2010-06-22 11:04:27