【发布时间】:2017-07-16 01:37:22
【问题描述】:
为什么我无法在我的频道中检索current_user,或者我应该如何检索current_user?
我用什么?
- Rails 5.0.1 --api (我没有任何意见也没有使用咖啡)
- 我使用 react-native 应用程序来测试这个 (无需授权即可正常工作)
- 我不使用 devise 进行身份验证(我使用 JWT 而不是使用 Knock,所以没有 cookie)
如rubydoc.info 中所述,尝试在我的 ActionCable 频道中获取 current_user
代码看起来像
class MessageChannel < ApplicationCable::Channel
identified_by :current_user
def subscribed
stream_from 'message_' + find_current_user_privileges
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
protected
def find_current_user_privileges
if current_user.has_role? :admin
'admin'
else
'user_' + current_user.id
end
end
end
运行它,我得到这个错误:
[NoMethodError - undefined method `identified_by' for MessageChannel:Class]
如果我删除identified_by :current_user,我会得到
[NameError - undefined local variable or method `current_user' for #<MessageChannel:0x7ace398>]
【问题讨论】:
标签: ruby-on-rails authentication actioncable