ruby-on-rails - Rails & Devise:删除用户帐户后聊天崩溃
问题描述
我将 Devise 用于我的应用程序身份验证,然后将聊天(对话/消息)功能集成到我的应用程序中。聊天工作正常,但是一旦我删除了与之聊天的用户,聊天页面就会崩溃,因为它“找不到用户 id = 1”(ActiveRecord 错误)
我的conversations controller
:
class ConversationsController < ApplicationController
before_action :authenticate_user!
def index
@users = User.all
@conversations = Conversation.all
end
def create
if Conversation.between(params[:sender_id], params[:recipient_id]).present?
@conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first
else
@conversation = Conversation.create!(conversation_params)
end
redirect_to conversation_messages_path(@conversation)
end
private
def conversation_params
params.permit(:sender_id, :recipient_id)
end
end
我的mesages controller
:
class MessagesController < ApplicationController
before_action do
@conversation = Conversation.find(params[:conversation_id])
end
def index
@messages = @conversation.messages
@message = @conversation.messages.new
end
def new
@message = @conversation.messages.new
end
def create
@message = @conversation.messages.new(message_params)
if @message.save
redirect_to conversation_messages_path(@conversation)
end
end
private
def message_params
params.require(:message).permit(:body, :user_id)
end
end
我的conversation model
:
class Conversation < ActiveRecord::Base
belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :between, -> (sender_id, recipient_id) do
where("(conversations.sender_id = ? AND conversations.recipient_id = ?) OR (conversations.sender_id = ? AND conversations.recipient_id = ?)", sender_id, recipient_id, recipient_id, sender_id)
end
end
我的message model
:
class Message < ActiveRecord::Base
belongs_to :conversation
belongs_to :user
validates_presence_of :body, :conversation_id, :user_id
def message_time
created_at.strftime("%m/%d/%y at %l:%M %p")
end
end
我添加了基于文档的标准方式设计,然后添加了聊天。
更新:
代码在这一行崩溃:(views/conversations/index.html.erb
)
<% @conversations.each do |conversation| %>
<% if conversation.sender_id == current_user.id || conversation.recipient_id == current_user.id %>
<% if conversation.sender_id == current_user.id %>
<% recipient = User.find(conversation.recipient_id) %>
<% else %>
<% recipient = User.find(conversation.sender_id) %>
<% end %>
<h3><%= link_to recipient.email, conversation_messages_path(conversation)%></h3>
<% end %>
<% end %>
具体在<% recipient = User.find(conversation.sender_id) %>
关于删除用户功能的工作原理,我没有编写代码,我只是让 Devise 做它的事情
解决方案
- 添加到对话中,您可以
belongs_to
选择:true。因为发件人/收件人可以被删除 - 处理它们是“nil”并显示“deleted”标签
- 尝试做
recipient = User.find_by(id: conversation.recipient_id) %> or conversation.recipient
(只使用关联)......并检查对象是否为零
推荐阅读
- python - 如何使用 Selenium 和 Python 点击启用 GWT 的元素
- javascript - 从数组jquery填充下拉值?
- python - How do you skip duplicate lines in the txt s?
- dataframe - 在 Julia DataFrame 中使用 for 循环根据另一个值创建变量
- git-bash - 如何在 Jupyterlab 上使用 Git Bash
- hibernate - Hibernate:是否可以仅检索匹配条件的子项?
- python - 使用在同一数据框行中找到的列表中的术语选择数据框行
- java - 为什么我在尝试将图像分配给我的 GridView 时收到此 NullPointerException?
- c# - 统一悬停建筑物,然后再放置它们进行游戏
- api - 我可以使用 Musixmatch API 访问专辑的封面吗?