2016-04-29 41 views
1

我想访问我的current_user方法,该方法在SessionsHelper中定义。我如何包括或要求它为ApplicationCable连接类?Rails5 ActionCable包含来自SessionsHelper的current_user方法

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中我包括的SessionHelper在渠道/ application_cable/connection.rb

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

,但不起作用。

回答

3

正如您在官方文档的notes中看到的那样,电缆不能访问会话。在笔记中引用的this文章中,您可以找到一种使用cookie管理认证的方法