2016-04-29 24 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管理認證的方法

相關問題