這是對teamtreehouse.com的練習。未定義的方法`full_name'爲零:NilClass
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :profile_name
validates :first_name, presence: true
validates :last_name, presence: true
validates :profile_name, presence: true,
uniqueness: true
has_many :statuses
def full_name
first_name + " " + last_name
end
end
status.rb
class Status < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
end
,我發現了以下錯誤。
undefined method 'full_name' for nil:NilClass
顯示/app/views/statuses/index.html.erb在行#9提出:
6:
7: <% @statuses.each do |status| %>
8: <div class="status">
9: <strong><%= status.user.full_name %></strong>
10: <p><%= status.content %></p>
11: <div class="meta">
12: <%= link_to time_ago_in_words(status.created_at) + " ago", status %>
我真的很想知道爲什麼。
感謝您花時間發佈此答案!拯救生命! – Starkers 2013-09-08 00:32:23