gpt4 book ai didi

ruby-on-rails - ActionCable 不更新页面

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:26 25 4
gpt4 key购买 nike

我正在创建一个 ruby​​ 应用程序,我正在尝试实现操作电缆,当用户发布帖子时,我希望为所有用户更新帖子索引。在我的 channels/posts.js 中我有:

App.posts = App.cable.subscriptions.create("PostsChannel", {
connected: function() {
// Called when the subscription is ready for use on the server
},

disconnected: function() {
// Called when the subscription has been terminated by the server
},

received: function(data) {
// Called when there's incoming data on the websocket for this channel
console.log(data)
$('#list').html("<%= escape_javascript render('list') %>")
}
});

在 PostRelayJob.rb 我有:

class PostRelayJob < ApplicationJob
queue_as :default

def perform(post)
ActionCable.server.broadcast "posts",
post: PostsController.render(list)
# Do something later
end
end

在 models/post.rb 中:

after_commit { PostRelayJob.perform_later(self) }

当我在服务器控制台中添加帖子时,我得到:

[ActiveJob] [PostRelayJob] [57126ec7-b091-48fc-91aa-2f940caa9421] Performing PostRelayJob from Async(default) with arguments: #<GlobalID:0x00000003aa10d8 @uri=#<URI::GID gid://today-i-have2/Post/15>>
[ActiveJob] Enqueued PostRelayJob (Job ID: 57126ec7-b091-48fc-91aa-2f940caa9421) to Async(default) with arguments: #<GlobalID:0x000000037af000 @uri=#<URI::GID gid://today-i-have2/Post/15>>
[ActiveJob] [PostRelayJob] [57126ec7-b091-48fc-91aa-2f940caa9421] Performed PostRelayJob from Async(default) in 3.66ms

Rendering posts/create.js.erb
Rendered posts/_list.html.erb (7.5ms)
Rendered posts/create.js.erb (9.0ms)

但是其他浏览器中的帖子没有更新,我是 Rails 的新手,所以非常感谢任何帮助。

最佳答案

我认为这种行为是在您在 Rails 控制台中创建 Post 对象 的情况下出现的。这在很大程度上取决于您的 ActionCable 配置,但我认为您没有在开发环境中使用 Redis。

开发环境的默认配置是使用async 适配器。此适配器仅在同一进程内工作。要启用跨进程通信,您需要一个备用适配器。

我的建议是安装 Redis(例如通过 brew redis),使用 redis-server 启动它并使用以下内容更新您的 config/cable.yml:

development:
adapter: redis
url: redis://localhost:6379

关于ruby-on-rails - ActionCable 不更新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39533307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com