3

我想访问在SessionsHelper中定义的current_user方法。我如何为ApplicationCable Connection 类包含或要求它?

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

    def connect
      if current_user
        self.current_user = current_user  
        logger.add_tags current_user.name
      end
    end
  end
end

给我

There was an exception - NoMethodError(undefined method `name' for nil:NilClass)

相当于 ApplicationController 我在 channels/application_cable/connection.rb 中包含了 SessionHelper

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    include SessionsHelper
    identified_by :current_user
    # (..)
  end
end

但不起作用。

4

1 回答 1

4

正如您在官方文档的注释中看到的那样,电缆无法访问会话。在注释中引用的这篇文章中,您可以找到一种使用 cookie 管理身份验证的方法

于 2016-04-29T21:10:35.547 回答