【问题标题】:AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this actionAbstractController::DoubleRenderError(在此操作中多次调用渲染和/或重定向
【发布时间】:2019-10-10 15:23:49
【问题描述】:

我创建了一个消息系统,一个用户发送消息,另一个用户接收实时更新的消息。它以前可以工作,但现在我出错了,没有任何渲染或刷新。

AbstractController::DoubleRenderError(渲染和/或重定向是 在此操作中多次调用。请注意,您只能 调用渲染或重定向,每个操作最多一次。另请注意 既不重定向也不渲染终止动作的执行,所以如果 你想在重定向后退出一个动作,你需要做 类似“redirect_to(...) and return”。):

这里是messages_controller.rb

class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation

def index
   if current_user == @conversation.sender || current_user == 
     @conversation.recipient
     @other = current_user == @conversation.sender ? 
     @conversation.recipient : @conversation.sender
     @messages = @conversation.messages.order("created_at DESC")
   else
   redirect_to conversations_path, alert: "You don't have permission to view this."
   end
   end

def create
  @message = @conversation.messages.new(message_params)
  @messages = @conversation.messages.order("created_at DESC")

 if @message.save
   ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
   redirect_to conversation_messages_path(@conversation)
 end

end

private

 def render_message(message)
   self.render(partial: 'messages/message', locals: {message: message})
 end

 def set_conversation
   @conversation = Conversation.find(params[:conversation_id])
 end

 def message_params
   params.require(:message).permit(:context, :user_id)
 end
end

我知道错误来自这里

 if @message.save
   ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
   redirect_to conversation_messages_path(@conversation)
 end

【问题讨论】:

  • 这个partial的目的是什么,self.render(partial: 'messages/message', locals: {message: message})

标签: ruby-on-rails ruby actioncable


【解决方案1】:

在 消息.rb

 after_create_commit { MessageJob.perform_later self }

并创建 MessageJob

class MessageJob < ApplicationJob
  queue_as :default

  def perform(message)
   ActionCable.server.broadcast "conversation_#{message.conversation.id}", message: render_message(message)
  end

  private


def render_message(message)
   ApplicationController.renderer.render(partial: 'messages/message', locals: {message: message})
 end

【讨论】:

    【解决方案2】:

    问题在于 Connection.rb 是这样的

    module ApplicationCable
    class Connection < ActionCable::Connection::Base
       identified_by :current_user
    
       def connect
       self.current_user = find_verified_user
       logger.add_tags 'ActionCable', current_user.name
       end
    
       protected
        def find_verified_user
          verified_user = User.find_by(id: cookies.signed['user.id'])
          if verified_user && cookies.signed['user.expires_at'] > Time.now
            verified_user
          else
            reject_unauthorized_connection
          end
        end
      end
    end
    

    当我改成这个时它起作用了

    module ApplicationCable
       class Connection < ActionCable::Connection::Base
       end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 2018-09-01
      • 2014-07-02
      相关资源
      最近更新 更多