gpt4 book ai didi

ruby-on-rails - 如何定期获取/更新 Rails 中的值?

转载 作者:数据小太阳 更新时间:2023-10-29 06:47:02 25 4
gpt4 key购买 nike

让我们来看一个场景:

counter 10 seconds
  • 用户访问了 show.html.erb
  • show.html.erbdatabase 获取值使用 <%= @post.value %> .
  • 计数器已启动,计数器的每次迭代都是针对 10 seconds 的.
  • 在每 10 seconds 之后我想更改 @post.value使用 utility类。
  • 更新@post.value在数据库中。
  • 刷新show.html.erb自动和<%= @post.value %>将显示更新后的值
  • 以上过程将循环运行,直到用户移动到其他页面。

如果我必须在代码中简化问题,那么它会像这样:

查看页面

<%= @post.value %>
<%= @post.name %>

Controller 方法

def show
@post = Post.find(params[:id])
end

def update
.... #It's empty right now
end

def fetching_value_from_PostUpdate(current_value)
.. # Now I need to update the value in database
end

实用类

我想更新 post.value在这个类中编写的方法的基础上。伪代码:

class PostUpdate
@@instance_variable

def initialize(post_value)
@@instance_variable = Marshal.load(Marshal.dump(post_value))
end

#Method required to calculate the value
def update_data_in_database
...
return data
end

问题

  • 我必须把柜台放在哪里?客户端还是服务器端?我不想使用 background jobs在 rails 上。
  • 我需要在 show 中进行哪些更改?方法,以便在每个间隔页面将自动刷新并将更新的值传递到 @post.value 中?

感谢您的帮助!

最佳答案

我会选择 Firebase而不是轮询服务器。

但是,如果您想定期轮询服务器,我只需要一个 JavaScript 方法,它每 10 秒执行一次,或者以您想要的任何时间间隔执行一次,并异步获取您想要的任何数据并随后更新的DOM。

此外,ruby wrapper for firebase api ,以防万一

关于ruby-on-rails - 如何定期获取/更新 Rails 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152339/

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