我有一些我想从 Action Cable Channel 调用的代码。
假设我有通知 channel ,我想打电话更新一些
values 并且它确实调用了该方法,但它没有调用另一个方法
写在应用程序 Controller 中。
说,
在我的 channel.rb 中,我正在使用:
class NotificationsChannel < ApplicationCable::Channel
include NotificationChannel
def some_method
create_notification(current_user, quiz)
end
end
NotificationChannel 问题代码:
module NotificationChannel
extend ActiveSupport::Concern
def create_notification(is_current_user, resource)
teacher_user_id = resource.lesson_plan.teacher.user_id
Notification.create(description: "A user with name #{get_user_name(is_current_user)} has liked the #{resource.title}", user_id: teacher_user_id, url: resource_misc_path(resource, "quiz_notification_like_check"))
end
end
现在,
第一个问题:
方法
get_user_name(is_current_user)
在关注和它写在
ApplicationController
.第二个问题:
resource_misc_path(resource, "quiz_notification_like_check" is written in some helper it does not call even helper method.
P.S:我也注意到有
_path
的路由和 _url
也无法在 Channel.rb 中访问任何合适的解决方法?
最佳答案
这里有一些关于这个主题的非常有用的答案:How do I get current_user in ActionCable rails-5-api app?
底线是在 Cable 中没有 session ,所以你不能真正使用你的 session 方法。
您必须直接使用 cookie 重写它们。
正如@Sajan 在他的回答中所说:“websocket 服务器没有 session ,但它可以读取与主应用程序相同的 cookie。”
关于ruby-on-rails - 如何访问 Rails 5 Action Cable Channels 中的关注点和应用程序 Controller 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42914624/