我有这个问题:
我有这段代码:
用户.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def full_name
"lalalala"
end
end
状态.rb:
class Status < ActiveRecord::Base
belongs_to :user
end
显示.html.erb:
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @status.user.full_name %>
</p>
<p>
<strong>Content:</strong>
<%= @status.content %>
</p>
<%= link_to 'Edit', edit_status_path(@status) %> |
<%= link_to 'Back', statuses_path %>
我在这里错过了什么?我无法访问 full_name 方法,但我不知道为什么。我是 Ruby-on-Rails 的新手,所以它一定是一些我不理解的简单细节。 rails 版本:4.1.5; ruby 版本:2.1.2
最佳答案
您的错误消息中包含您需要的所有内容。您的 Status
记录没有关联的 User
记录,因此 @status.user
返回 nil
。
关于ruby-on-rails - Belongs_to 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25782810/