gpt4 book ai didi

ruby-on-rails - Rails 变量第一次加载然后为零!

转载 作者:行者123 更新时间:2023-12-04 03:38:33 25 4
gpt4 key购买 nike

我收到以下错误:

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 %>

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

Controller 看起来像这样:

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

关于ruby-on-rails - Rails 变量第一次加载然后为零!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3512841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com