【问题标题】:Rails5 ActionCable include current_user method from SessionsHelperRails5 ActionCable 包含来自 SessionsHelper 的 current_user 方法
【发布时间】:2016-04-29 20:56:34
【问题描述】:

我想访问我在 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)

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

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

但不起作用。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 actioncable


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      相关资源
      最近更新 更多