我收到以下错误:

ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?) on line #24 of app/views/index/index.html.erb:
21: <% @achievements.each do |achievement| %>
22:     <%= achievement.name %>
23:     <%= achievement.level %>
24:     by <%= achievement.user.username %><br/>
25: <% end %>


奇怪的是,当第一次加载索引页时,没有任何问题。刷新时,出现上述错误。

控制器如下所示:

class IndexController < ApplicationController
    def index
        @achievements = Achievement.find(:all)
    end
end


与缓存有关吗?还是使用过多的内存?如果是这样,我可以其他方式加载用户名吗?我很困惑!

最佳答案

尝试通过在搜索结果中添加“:include =>:user”来渴望加载用户:

class IndexController < ApplicationController
    def index
        @achievements = Achievement.find(:all, :include => :user)
    end
end

10-08 04:40