我不确定我的代码有什么问题:
class ZombieController < ApplicationController
def index
@zombies = Zombie.all
respond_to do |format|
#format.json {render json: @rotting_zombies}
format.html
end
end
end
class Zombie < ActiveRecord::Base
attr_accessible :name, :rotting, :age
has_many :tweets
has_one :brain, dependent: :destroy
scope :rotting, where(rotting: true)
scope :fresh, where("age < 30")
scope :recent,order('created_at desc').limit(3)
end
class Brain < ActiveRecord::Base
attr_accessible :flavor, :status, :zombie_id
belongs_to :zombie
end
在 Zombie index 视图中,我将僵尸名称与大脑风味渲染如下:
<h1>List</h1>
<table>
<tr>
<td>Name</td>
<td></td>
<td>Flavor</td>
</tr>
<% @zombies.each do |zombie|%>
<tr>
<td><%= zombie.name %></td>
<td><%= zombie.brain.flavor %></td>
</tr>
<% end %>
</table>
我收到的错误是undefined method
nil:NilClass 的味道。这里可能有什么问题?据我所知,我正确定义了僵尸和大脑模型的关系。