简体   繁体   English

使用AJAX轮询数据库更改并将新更改附加到Rails中的视图

[英]Polling for changes to database and append new changes to the view in Rails using AJAX

I'm having some trouble appending content from the database to the view when new content is added to a specific table. 将新内容添加到特定表时,将数据库中的内容追加到视图时遇到了一些麻烦。 Currently, every number of minutes the database is updated with new content by a cron job. 当前,cron作业每隔几分钟便用新内容更新数据库。

The goal in the end is to have the page displaying this content being added to the database to display the latest content without refreshing the page. 最后的目标是将显示此内容的页面添加到数据库中,以显示最新内容,而无需刷新页面。 The only way I can think about how this could be done is with AJAX. 我能想到的唯一方法是使用AJAX。 I have looked into Railscast 229 where they talk about how to do this but it wasn't making a lot of sense. 我查看了Railscast 229,他们在其中谈论如何执行此操作,但这没有多大意义。

If seeing code samples will help then I can provide some of that as well. 如果看到代码示例会有所帮助,那么我也可以提供一些示例。

Thanks in advance! 提前致谢!

With a typical multi process rails setup polling ist your best option. 使用典型的多进程导轨设置轮询是您的最佳选择。

There also 2 alternatives. 还有2种选择。 Websockets and Server Sent Events which can be used with multithreaded or evented (NIO) deploys. 可以与多线程或事件(NIO)部署一起使用的Websocket和服务器已发送事件。

In both cases there is long open connection between the browser client and the servier, through which the server can publish updates, the client subscribes to. 在这两种情况下,浏览器客户端和服务器之间的连接都是长期开放的,服务器可以通过该连接发布客户端订阅的更新。 Server Sent Events are actually pretty easy in rails. 服务器发送事件实际上在Rails中非常容易。 Checkout Mini-chat with Rails and Server-Sent Events 带有Rails和服务器发送事件的 Checkout 迷你聊天

See also this answer about different types of concurrency models 另请参阅有关不同类型的并发模型的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM