【发布时间】:2019-03-31 12:23:16
【问题描述】:
我正在制作一个 Rails 应用程序,使用 postgresql 作为数据库,您可以在其中与用户联系并从他们的帖子中向他们发送消息。我的种子文件创建了 10 个测试用户,我正在检查向其他用户发送消息的功能,但是没有用户可以进行测试,所以我跳上 rails 控制台并清除了会话和消息表,这样我就可以删除数据库中的所有关系但从那以后我一直收到这个错误:
ActiveRecord::RecordNotFound in PagesController#index
Couldn't find all Private::Conversations with 'id': (14, 2, 3, 5, 6, 8, 9,
10, 11, 1) (found 3 results, but was looking for 10).
这是我的 PagesController 索引操作中的代码:
def index
@hobby_posts = Post.by_branch('hobby').limit(8)
@study_posts = Post.by_branch('study').limit(8)
@team_posts = Post.by_branch('team').limit(8)
end
为什么总是弹出这个错误?
我尝试完全删除数据库,重新创建数据库,重新运行迁移,但这似乎并没有清除对话
这是我的 ApplicationController 中的代码:
def opened_conversations_windows
if user_signed_in?
session[:private_conversations] ||= []
@private_conversations_windows = Private::Conversation.includes(:recipient, :messages)
.find(session[:private_conversations])
else
@private_conversations_windows = []
end
end
当我将 if 行更改为时,代码似乎“工作”
if (user_signed_in? && @private_conversations_windows)
但是当我这样做并与用户创建新聊天时,新聊天关系的创建会命中数据库,但聊天窗口不显示。 更新:
class Private::ConversationsController < ApplicationController
def create
recipient_id = Post.find(params[:post_id]).user.id
@conversation = Private::Conversation.new(sender_id: current_user.id,
recipient_id: recipient_id)
if @conversation.save
Private::Message.create(user_id: current_user.id,
conversation_id: @conversation.id,
body: params[:message_body])
add_to_conversations unless already_added?
respond_to do |format|
format.js {render partial: 'posts/show/contact_user/message_form/success'}
end
else
respond_to do |format|
format.js {render partial: 'posts/show/contact_user/message_form/fail'}
end
end
end
def close
@conversation_id = params[:id].to_i
session[:private_conversations].delete(@conversation_id)
respond_to do |format|
format.js
end
end
private
def add_to_conversations
session[:private_conversations] ||= []
session[:private_conversations] << @conversation.id
end
def already_added?
session[:private_conversations].include?(@conversation.id)
end
end
【问题讨论】:
标签: ruby-on-rails ruby database postgresql postgresql-9.3