Rails 自定义消息系统,未正确创建对话

Rails custom message system, conversation is not created properly

所以我创建了一个基于https://www.sitepoint.com/build-a-messaging-system-with-rails-and-actioncable/

的自定义消息系统

在我遇到无法正确创建新对话的错误之前,它一直运行良好。 (对话在我的代码中被命名为insconversation)

class PersonalMessagesController
...
  def create
    @insconversation ||= Insconversation.create(author_id: current_user.id, //This part seems to be having issues
                                      receiver_id: @receiver.id)
    @personal_message = current_user.personal_messages.build(personal_message_params)
    @personal_message.insconversation_id = @insconversation.id
    @personal_message.save!

    flash[:success] = "Your message was sent!"
   redirect_to insconversation_path(@insconversation)
  end
...

消息本身保存到 Mysql 数据库中,但它的父 "insconversation_id" 保留为空,同样在 "insconversation" table 上没有添加新对话。我使用了 render :text => 和 insconversation,它的 id 都是 null,(因为创建失败)。

我收到的错误信息在redirect_to部分 没有路由匹配 {:action=>"show", :controller=>"insconversations", :id=>nil} 缺少必需的键:[:id]

以及个人消息查看

view/Personal_Message
<h1>New message to <%= @receiver.name %></h1>   //receiver is properly identified

<%= form_for @personal_message do |f| %>
  <%= hidden_field_tag 'receiver_id', @receiver.id %>

  <%= f.label :body %>
  <%= f.text_area :body, size: "60x3", required:true %>

  <%= f.submit %>

<% end %>

对话控制器

class InsconversationsController < ApplicationController
  before_action :set_insconversation, except: [:index]
  before_action :check_participating!, except: [:index]

  def index
    @insconversations = Insconversation.participating(current_user).order('updated_at DESC')
  end

  def show
    @insconversation = Insconversation.find_by(id: params[:id])
    @personal_message = PersonalMessage.new
  end

  private

  def set_insconversation
    @insconversation = Insconversation.find_by(id: params[:id])
  end

  def check_participating!
    redirect_to root_path unless @insconversation && @insconversation.participates?(current_user)
  end
end

编辑:

我刚刚查看了 SQL 日志

 Insconversation Exists (0.2ms)  SELECT  1 AS one FROM `insconversations` WHERE `insconversations`.`author_id` = 2 AND `insconversations`.`receiver_id` = 3 LIMIT 1
   (0.2ms)  ROLLBACK
   (0.2ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `personal_messages` (`body`, `user_id`, `created_at`, `updated_at`) VALUES ('8', 2, '2017-04-24 00:31:11', '2017-04-24 00:31:11')

系统似乎认为对话已经存在。

模型上的 ID 由 Rails 自动设置,因此您评论说遇到问题的代码部分并不是实际问题。问题出在以下行:

   redirect_to insconversation_path(@insconversation)

消息指出您缺少对话 ID 的参数。您只需要对此行稍作调整即可获取 ID。

redirect_to insconversation_path(@insconversation.id)

或者你可以

redirect_to @insconversation

和 Rails 将推断其余部分。

你还需要这个修复吗?我有同样的问题。使用这个:

class PersonalMessagesController < ApplicationController
  before_action :find_conversation!

  def new
    redirect_to conversation_path(@conversation) and return if @conversation
    @personal_message = current_user.personal_messages.build
  end

  def create
    @conversation ||= Conversation.create(author_id: current_user.id,
                                      receiver_id: @receiver.id)
    @personal_message = current_user.personal_messages.build(personal_message_params)
    @personal_message.conversation_id = @conversation.id
    @personal_message.save!

    flash[:success] = "Your message was sent!"
    redirect_to conversation_path(@conversation)
  end

  private

  def personal_message_params
    params.require(:personal_message).permit(:body)
  end

  def find_conversation!
    if params[:receiver_id]
      @receiver = User.find_by(id: params[:receiver_id])
      redirect_to(root_path) and return unless @receiver
      @conversation = Conversation.between(current_user.id, @receiver.id)[0]
    else
      @conversation = Conversation.find_by(id: params[:conversation_id])
      redirect_to(root_path) and return unless @conversation && @conversation.participates?(current_user)
    end
  end
end