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