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

时间:2017-04-23 18:59:21

标签: ruby-on-rails messaging

所以我创建了一个基于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"保留为null,类似于" insconversation"表没有添加新的对话。我用了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')

系统似乎认为Inconversation已经存在。

2 个答案:

答案 0 :(得分:1)

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

   redirect_to insconversation_path(@insconversation)

该消息指出您缺少insconversation ID的参数。您只需稍微调整此行即可获取ID。

redirect_to insconversation_path(@insconversation.id)

或者你可以做到

redirect_to @insconversation

和Rails将推断其余部分。

答案 1 :(得分:0)

你还需要这个吗?我遇到了同样的问题。使用此:

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