1

我遵循教程并在我的 Rails 应用程序中添加了消息传递功能。
昨天,我删除了某些用户。现在,当我转到我的消息链接时,我收到此错误消息“我们很抱歉,但出了点问题。如果您是应用程序所有者,请查看日志以获取更多信息。” 通常在出现错误时显示。

看了日志,发现一定是删除了某个发消息的人。
我认为它正在尝试检索不存在的用户的消息。

ActionView::Template::Error (undefined method `avatar' for nil:NilClass):

我该如何解决这个问题?
谁能分享如何删除所有消息并重新开始?因为消息并不重要。
如何清除所有邮箱表并重新开始?请帮忙。

这是日志。

2015-05-16T12:17:13.398640+00:00 app[web.1]:   Rendered conversations/_participants.html.erb (17.3ms)
2015-05-16T12:17:13.407905+00:00 app[web.1]:   CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2225]]
2015-05-16T12:17:13.434486+00:00 app[web.1]:    (16.2ms)  SELECT COUNT(*) FROM "mailboxer_receipts" INNER JOIN "mailboxer_notifications" ON "mailboxer_notifications"."id" = "mailboxer_receipts"."notification_id" AND "mailboxer_notifications"."type" IN ('Mailboxer::Message') WHERE "mailboxer_notifications"."conversation_id" = 973 AND "mailboxer_receipts"."receiver_id" = $1 AND "mailboxer_receipts"."receiver_type" = $2 AND "mailboxer_receipts"."trashed" = 'f' AND "mailboxer_receipts"."is_read" = 'f'  [["receiver_id", 1464], ["receiver_type", "User"]]
2015-05-16T12:17:13.453617+00:00 app[web.1]:   Mailboxer::Receipt Load (1.1ms)  SELECT "mailboxer_receipts".* FROM "mailboxer_receipts" WHERE "mailboxer_receipts"."notification_id" = $1  [["notification_id", 1209]]
2015-05-16T12:17:13.456255+00:00 app[web.1]:   Rendered conversations/_participants.html.erb (20.5ms)
2015-05-16T12:17:13.474657+00:00 heroku[router]: at=info method=GET path="/conversations" host=www.abc.com request_id=991e2d24-c507-4e5f-9b50-5678e2e3aa2c fwd="160.3.94.130" dyno=web.1 connect=1ms service=276ms status=500 bytes=1786
2015-05-16T12:17:13.569003+00:00 heroku[router]: at=info method=GET path="/speak" host=www.abc.com request_id=c1a056f3-4431-4c90-84b8-01c285e606c1 fwd="122.174.14.148" dyno=web.2 connect=1ms service=37ms status=200 bytes=35624
2015-05-16T12:17:13.459247+00:00 app[web.1]:   Mailboxer::Message Load (1.8ms)  SELECT  "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = $1  ORDER BY created_at DESC LIMIT 1  [["conversation_id", 782]]
2015-05-16T12:17:13.469138+00:00 app[web.1]:   Rendered conversations/_conversation.html.erb (233.8ms)
2015-05-16T12:17:13.469485+00:00 app[web.1]:   Rendered conversations/index.html.erb within layouts/application (249.4ms)
2015-05-16T12:17:13.469543+00:00 app[web.1]: Completed 500 Internal Server Error in 261ms
2015-05-16T12:17:13.473924+00:00 app[web.1]: 
2015-05-16T12:17:13.473928+00:00 app[web.1]: ActionView::Template::Error (undefined method `avatar' for nil:NilClass):
2015-05-16T12:17:13.473930+00:00 app[web.1]:     16:       
2015-05-16T12:17:13.473932+00:00 app[web.1]:     17:       <span class="messageuser">
2015-05-16T12:17:13.473933+00:00 app[web.1]:     18:         <%#= conversation.last_message.sender.name %>
2015-05-16T12:17:13.473936+00:00 app[web.1]:     19:         <%= link_to image_tag(conversation.last_message.sender.avatar.url(:mini), class: 'img-rounded'), user_path(conversation.last_message.sender) %>
2015-05-16T12:17:13.473944+00:00 app[web.1]:     20:         <%= link_to conversation.last_message.sender.name, user_path(conversation.last_message.sender) %>
2015-05-16T12:17:13.473946+00:00 app[web.1]:     21:       </span>
2015-05-16T12:17:13.473948+00:00 app[web.1]:     22:       
2015-05-16T12:17:13.473951+00:00 app[web.1]:   app/views/conversations/_conversation.html.erb:19:in `_app_views_conversations__conversation_html_erb___2085506579815819501_69959535266760'
2015-05-16T12:17:13.473952+00:00 app[web.1]:   app/views/conversations/index.html.erb:35:in `_app_views_conversations_index_html_erb___1748297580391940998_69959535151580'
2015-05-16T12:17:13.473954+00:00 app[web.1]: 
2015-05-16T12:17:13.473957+00:00 app[web.1]: 
2015-05-16T12:17:13.572052+00:00 app[web.1]: Started GET "/conversations" for 160.3.94.130 at 2015-05-16 12:17:13 +0000
2015-05-16T12:17:13.579974+00:00 app[web.1]:   User Load (1.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1464]]
2015-05-16T12:17:13.585203+00:00 app[web.1]:    (0.9ms)  SELECT COUNT(*) FROM "user_learnings" WHERE "user_learnings"."user_id" = $1  [["user_id", 1464]]
2015-05-16T12:17:13.637517+00:00 app[web.1]:    (3.1ms)  SELECT DISTINCT COUNT(DISTINCT "mailboxer_conversations"."id") FROM "mailboxer_conversations" INNER JOIN "mailboxer_notifications" ON "mailboxer_notifications"."conversation_id" = "mailboxer_conversations"."id" AND "mailboxer_notifications"."type" IN ('Mailboxer::Message') INNER JOIN "mailboxer_receipts" ON "mailboxer_receipts"."notification_id" = "mailboxer_notifications"."id" WHERE "mailboxer_notifications"."type" = 'Mailboxer::Message' AND "mailboxer_receipts"."receiver_id" = $1 AND "mailboxer_receipts"."receiver_type" = $2 AND "mailboxer_receipts"."mailbox_type" = $3 AND "mailboxer_receipts"."trashed" = 'f' AND "mailboxer_receipts"."deleted" = 'f'  [["receiver_id", 1464], ["receiver_type", "User"], ["mailbox_type", "inbox"]]
2015-05-16T12:17:13.678884+00:00 app[web.1]:   Mailboxer::Receipt Load (1.2ms)  SELECT "mailboxer_receipts".* FROM "mailboxer_receipts" WHERE "mailboxer_receipts"."notification_id" = $1  [["notification_id", 1443]]
2015-05-16T12:17:13.698778+00:00 app[web.1]:   User Load (4.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1464]]
2015-05-16T12:17:13.464477+00:00 app[web.1]:   User Load (1.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1937]]
4

3 回答 3

2

我变得不耐烦了,把他们都毁了。

irb(main):002:0> Mailboxer::Conversation.destroy_all

现在错误消失了,我可以发送消息了。
我不知道如何删除我最近删除的 800 个用户的对话。

于 2015-05-16T18:43:10.240 回答
1

你说的对。应用程序正在调用不存在的用户的方法。例如:```

<%= link_to conversation.last_message.sender.name...

(conversation.last_message) 已保存,但发件人不存在。

如果您想从头开始启动数据库,您可以运行 (rake db:reset),这将删除创建和迁移您的数据库。

要解决此问题,请创建一个在用户每次取消或删除他/她的帐户时运行的方法,并删除该用户的对话。试试这是你的控制台(rails c),它将删除第一个用户的对话。```

User.first.mailbox.conversations.each do |i| i.delete end

如果您仍想向未取消其帐户的其他用户显示对话。您必须将该名称保存在其他地方或向用户显示发件人删除了他的帐户。```

<% if conversation.sender == null %>
  <%= conversation.last_message %>
  <p>User of this conversation deleted his/her account</p>
<%else%>
  <%= conversation.last_message %>
  <%= conversation.last_message.sender.name %>
<% end %>
于 2015-05-16T18:16:38.983 回答
0

听起来像是一种未优化的方法是在模型上循环遍历添加了消息传递功能的模型acts_as_messageable..likeMyModel.each do |mod| mod.mailbox.conversations.destroy_all end

于 2015-05-16T16:47:44.827 回答