【问题标题】:Heroku can't find user but works in devHeroku 找不到用户,但在开发中工作
【发布时间】:2014-04-29 16:50:49
【问题描述】:

我的 heroku 部署遇到了问题。这是日志:

   2014-04-29T16:41:59.782999+00:00 app[web.1]: Started GET "/lines/33/edit" for 94.174.113.168 at 2014-04-29 16:41:59 +0000
    2014-04-29T16:41:59.782933+00:00 app[web.1]: Started GET "/lines/33/edit" for 94.174.113.168 at 2014-04-29 16:41:59 +0000
    2014-04-29T16:41:59.804819+00:00 app[web.1]: 
    2014-04-29T16:41:59.804823+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with id=0):
    2014-04-29T16:41:59.804826+00:00 app[web.1]:   app/views/lines/_history.html.erb:7:in `block in _app_views_lines__history_html_erb__3033746240219592358_69949428301200'
    2014-04-29T16:41:59.804824+00:00 app[web.1]:   config/initializers/paper_trail.rb:4:in `user'
    2014-04-29T16:41:59.804854+00:00 app[web.1]:   app/views/lines/_history.html.erb:4:in `_app_views_lines__history_html_erb__3033746240219592358_69949428301200'
    2014-04-29T16:41:59.804828+00:00 app[web.1]:   app/views/lines/_history.html.erb:4:in `each'
    2014-04-29T16:41:59.804856+00:00 app[web.1]:   app/views/lines/edit.html.erb:16:in `_app_views_lines_edit_html_erb__4129892984524241027_69949427894700'
    2014-04-29T16:41:59.804869+00:00 app[web.1]:   app/views/lines/_history.html.erb:4:in `each'
    2014-04-29T16:41:59.804871+00:00 app[web.1]:   app/views/lines/_history.html.erb:4:in `_app_views_lines__history_html_erb__3033746240219592358_69949428301200'
    2014-04-29T16:41:59.804872+00:00 app[web.1]:   app/views/lines/edit.html.erb:16:in `_app_views_lines_edit_html_erb__4129892984524241027_69949427894700'
    2014-04-29T16:41:59.804875+00:00 app[web.1]: 
    2014-04-29T16:41:59.804874+00:00 app[web.1]: 
    2014-04-29T16:41:59.804857+00:00 app[web.1]: 
    2014-04-29T16:41:59.804858+00:00 app[web.1]: 
    2014-04-29T16:41:59.804864+00:00 app[web.1]: 
    2014-04-29T16:41:59.804865+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with id=0):
    2014-04-29T16:41:59.804866+00:00 app[web.1]:   config/initializers/paper_trail.rb:4:in `user'
    2014-04-29T16:41:59.804868+00:00 app[web.1]:   app/views/lines/_history.html.erb:7:in `block in _app_views_lines__history_html_erb__3033746240219592358_69949428301200'

有问题的初始化程序如下:

module PaperTrail
  class Version < ActiveRecord::Base
    def user
      User.find self.whodunnit.to_i
    end

    def nextversion
        self.next
    end
  end
end

在 _history 视图中调用如下:

<%= gravatar_for version.user %><%= link_to  version.user.username, user_path(version.user) %>

这在开发中运行良好,并且在大多数编辑页面上似乎在生产中运行良好,但在一个我遇到此错误。我想在控制台中检查版本,但似乎我根本无法在控制台中访问 paper_trail 版本,我只能假设这是因为我没有版本模型,只有一个表和一个控制器。

我该如何解决这个问题?

【问题讨论】:

  • 我有 30 多个 @lines,但只有一个会抛出此错误。

标签: ruby-on-rails heroku ruby-on-rails-4 paper-trail-gem


【解决方案1】:

您收到以下错误

ActiveRecord::RecordNotFound (Couldn't find User with id=0)

关于以下方法

def user
  puts "Value of whodunnit is -----------> #{self.whodunnit}"
  User.find self.whodunnit.to_i
end

中调用
<%= gravatar_for version.user %><%= link_to  version.user.username, user_path(version.user) %>

因为self.whodunnit.to_i 正在返回0。这意味着,whodunnit""nil0。请设置whodunnit 属性的正确值。另外,您可以通过在PaperTrail::Version# user 方法中添加loggerputs 语句来检查whodunnit 的当前值。

【讨论】:

  • 如果没有控制台访问权限,我无法设置 whodunnit 的值。如果页面由于错误而无法加载,恐怕我看不到 puts 语句会显示在哪里。
  • 好的,所以日志现在有2014-04-29T17:51:16.264805+00:00 app[web.1]: Value of whodunnit is -----------&gt; ,后面什么都没有。所以它是零,对吧?我想我需要更改我的代码以便它可以处理 nil?
  • YES..这就是你需要做的。
  • 我可以在那些使用用户的调用中添加 .try 吗?
  • try 方法仅在调用者为 nil 时有效。例如version.try(:user) 如果版本不为零,那么只会调用用户方法。
【解决方案2】:

您可能没有运行您的 seed.rb 文件 (heroku run rake db:seed) 来创建用户,或者您只是在寻找一个不再定义的用户(例如删除)。

当您删除一个 id=0 的用户时,下一个用户的 id=1。

只是可以肯定,o 建议你在你的控制台上输入(heroku 运行控制台)并尝试自己找到它(Client.find(0))或尝试列出所有这些(Client.all)以查看是否定义了 id=0 的用户。

希望对你有帮助

【讨论】:

  • 我只创建了一个用户,并且该用户创建了所有编辑,因此 whodunnit 应该始终是用户 id=1。
  • 我看不到 whodunnit 列中的内容,因为我似乎无法在控制台中访问版本。
  • 如果是您的,您可以访问... 尝试列出所有用户。事实是 id=0 的用户不存在。发生这种情况有两种方法:首先,您的代码在某个变量中加一,我认为这会偶然发生,所以我认为您刚刚创建了另一个用户。我坚持认为您需要在控制台上列出所有用户,至少看看您是否只有一个用户...
  • User.first 和 User.last 返回相同的用户 #1。它必须是空的。很奇怪。
  • 数据库的第一个用户必须是 id=0。检查您的 seed.rb 是否首先创建了一些用户。也许重置您的数据库可以帮助您消除错误。
猜你喜欢
  • 2011-08-20
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多