Ruby on rails 在状态中获取命名错误#显示
我正在用Ruby on Rails构建一个社交媒体网站,并遵循teamtreehouse.com的指示。每次我做一个新的状态或转到索引页,我都会收到这条消息Ruby on rails 在状态中获取命名错误#显示,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在用Ruby on Rails构建一个社交媒体网站,并遵循teamtreehouse.com的指示。每次我做一个新的状态或转到索引页,我都会收到这条消息 undefined method `full_name' for nil:NilClass <p> <strong>Name:</strong> <%= @status.user.full_name %> </p> nil:NilClass的未定义方法“
undefined method `full_name' for nil:NilClass
<p>
<strong>Name:</strong>
<%= @status.user.full_name %>
</p>
nil:NilClass的未定义方法“full_name”
名称:
我给我的页面编码,用这个来显示全名
<%= @status.user.full_name %>
及
用于索引页
我也在我的项目中包含了所有这些代码片段
状态.rb
class Status < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :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
# attr_accessible :title, :body
def full_name
first_name + " " + last_name
end
end
类状态
user.rb
class Status < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :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
# attr_accessible :title, :body
def full_name
first_name + " " + last_name
end
end
class用户
我尝试过使用
rakedb:reset
和rakedb:migrate
但似乎没有任何方法可以解决错误问题。谢谢你的帮助 看起来您需要在用户
模型中包含has\u many:status
您可能还希望使用以下方法:
类状态
这应该允许您根据错误调用@status.author\u full\u name
undefined method `full_name' for nil:NilClass
也就是说
@status.user = nil
i、 例如,您正在查看的特定状态实例(@status
)没有关联的用户记录
您可以通过转到rails控制台
Status.find(pass_the_id) ## pass the id of @status instance
您将注意到,对于该记录,user\u id
为nil
将该值设置为现有的用户id
,然后重试
注意:您可能需要更深入地了解如何在应用程序中存储状态。我想您没有在那里存储
user\u id
属性刚刚添加了该属性,但仍然收到相同的错误消息show-us-your schema。如果调用实际属性(first\u name
)等,是否会引发错误?请确保正在查看的状态对象具有关联的用户。看起来没有。如果特定状态记录的user_id=nil,请检入rails控制台