【发布时间】:2018-10-16 10:44:25
【问题描述】:
我不明白actioncable。我想让用户订阅一个看起来像这样的PostChannel,
class PostNotificationsChannel < ApplicationCable::Channel
def subscribed
stream_from params[:post_id]
end
def unsubscribed
stop_all_streams
end
end
然后我有 js
/javascripts/channels/post_notifications.js
$(document).on('turbolinks:load', function() {
$("a.post_subscribe").click(function subscribeToPost() {
var post_id = $(this).parents("li").attr("class").split(" ")[0]; // Get the post id off the page
App.post_notifications = App.cable.subscriptions.create({channel: "PostNotificationsChannel", post_id: post_id}, {
connected: function() {},
disconnected: function() {},
received: function(data) {}
});
});
});
因此,您单击 .post_subscribe 锚标记,它会获取我在页面上拥有的那个帖子 HTML 类的值,然后在订阅创建的 params 哈希中使用它。这一切都很好,而且当该帖子发生某些操作时,单击它的用户将获得更新。但是,如果您刷新页面,则该连接无法将该消费者与该帖子重新连接,因为散列中没有参数。所以,我想知道如何摆脱这种情况。
此外,即使在 rails 指南页面上,它也会显示此 here 所以他们正在传递参数但是当页面重新加载时如何保持连接?
【问题讨论】:
标签: javascript ruby-on-rails actioncable