“ ActionCable连接未打开!`this.isActionCableConnectionOpen必须在调用`this.stimulate之前返回true”

问题描述

在使用导轨6进行刺激反射时出现上述错误

  <a href="#"
  data-reflex="click->VotesReflex#increment"
  data-step="1" 
  data-count="<%= @count.to_i %>"
  >Increment <%= @count.to_i %></a>

 class VotesReflex < ApplicationReflex
  delegate :current_user,to: :connection
  def increment
    @count = element.dataset[:count].to_i + element.dataset[:step].to_i
  end
end

解决方法

您应标识与current_user的连接以打开连接。假设您正在使用devise进行身份验证。

这是您应该做的。

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    private

    def find_verified_user
      # rubocop:disable Lint/AssignmentInCondition
      if verified_user = User.find_by(id: cookies.signed['user.id'])
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

在初始化程序中,创建warden_cookies.rb

Warden::Manager.after_set_user do |user,auth,opts|
  scope = opts[:scope]
  auth.cookies.signed["#{scope}.id"] = user.id
  auth.cookies.signed["#{scope}.expires_at"] = 30.minutes.from_now
end

Warden::Manager.before_logout do |_user,opts|
  scope = opts[:scope]
  auth.cookies.signed["#{scope}.id"] = nil
  auth.cookies.signed["#{scope}.expires_at"] = nil
end

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...