验证失败:发件人必须存在,收件人必须存在

Validation failed: Sender must exist, Recipient must exist

我正在尝试在我的网站上创建一个消息部分,用户可以在其中从用户显示页面向其他用户发送消息,但我一直收到 ActiveRecord::RecordInvalid in UsersController#show

Validation failed: Sender must exist, Recipient must exist.

我不知道我在这里做错了什么,甚至不知道如何去做,

我有一个运行良好的普通聊天部分,下面是其中的一些代码片段。

这是我的对话模型

class Conversation < ApplicationRecord
  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

在消息模型中,app/models/message.rb: 我有

class Message < ApplicationRecord
  belongs_to :conversation
  belongs_to :user
  validates_presence_of :body, :conversation_id, :user_id
end

在对话控制器中,app/controllers/conversations_controller.rb:我有

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

app/controllers/messages_controller.rb: 我有

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

在app/views/conversations/index.html.erb

<h1>My Inbox</h1>
<h1>All Conversations:</h1>
<% @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 %>
<h1>All Users:</h1>
<% @users.each do |user| %>
 <% if user.id != current_user.id %><h3>
 <%= link_to user.email, conversations_path(sender_id: current_user.id, recipient_id: user.id), method: "post"%></h3>
 <% end %>
<% end %>

在app/views/messages/index.html.erb我有

<% @messages.each do |message| %>
 <% if message.body %>
  <% user = User.find(message.user_id) %>
  <%= user.email %><%= message.message_time %>
  <%= message.body %>
 <% end %>
<% end %>
<%= form_for [@conversation, @message] do |f| %>
 <%= f.text_area :body %>
 <%= f.text_field :user_id, value: current_user.id, type: "hidden" %>
 <%= f.submit "Send Reply" %>
<% end %>

这个聊天部分运行良好,但现在我希望用户通过转到特定用户显示页面发送消息,点击发送消息,它应该将用户带到两个用户之间的消息页面,用户可以互相聊天,

这是我试过的

我的用户控制器:

class UsersController < ApplicationController
    def show
        @user = User.find(params[:id])

        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

我的users/show.html.erb

<%#= link_to "Send Message", conversations_path(sender_id: current_user.id, recipient_id: teacher.id), method: "post"%></h3>

当我尝试 运行 时,顶部出现错误,我认为我做的不对;如何让用户从用户显示页面向其他用户发送消息?

我认为您没有在 UsersController#show 中呈现模板。尽量保持简单,让它渲染展示模板,然后你可以使用 link 和 post 方法来创建对话

class UsersController < ApplicationController
    def show
        @user = User.find(params[:id])
    end
end

此外 teacher 未在您的视图中定义,请尝试将其替换为 @user