如何定期获取/更新rails中的值?

时间:2021-02-28 11:24:41

Let's take a scenario:

我们来看一个场景:

counter 10 seconds
  • User visited show.html.erb page
  • 用户访问了show.html.erb页面
  • show.html.erb fetch the value from database using <%= @post.value %>.
  • show.html.erb使用<%= @ post.value%>从数据库中获取值。
  • Counter started and each iteration of counter is for 10 seconds.
  • 计数器启动,计数器的每次迭代都持续10秒。
  • After every 10 seconds I wanted to change the @post.value using utility class.
  • 每10秒钟后,我想使用实用程序类更改@ post.value。
  • update the @post.value in database.
  • 更新数据库中的@ post.value。
  • Refresh the show.html.erb automatically and <%= @post.value %> will show the updated value
  • 自动刷新show.html.erb,<%= @ post.value%>将显示更新的值
  • Above process will be run in loop until user move to other pages.
  • 上面的过程将循环运行,直到用户移动到其他页面。

If I have to simplify the problem in code then it would like this:

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

View Page

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

Controller Method

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

Utility class

I want to update the post.value on the basis of method written in this class. Pseudo code:

我想根据这个类中编写的方法更新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

Questions

问题

  • Where do I have to put the counter? client side or server side? I don't want to use background jobs in rails.
  • 我在哪里放柜台?客户端还是服务器端?我不想在rails中使用后台作业。
  • What changes do I need to make in show method so that after every interval page will refresh automatically and pass the updated value in @post.value?
  • 我需要在show方法中进行哪些更改,以便在每个间隔页面之后自动刷新并在@ post.value中传递更新的值?

Thanks for your help!

谢谢你的帮助!

2 个解决方案

#1


2  

I would go with Firebase as opposed to polling the server.

我会使用Firebase而不是轮询服务器。

However, if you're wanting to poll the server periodically, I would just have a JavaScript method which executes every 10 seconds, or at whatever interval you'd like, and fetches whatever data you'd like asynchronously and subsequently updates the DOM.

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

Also, ruby wrapper for firebase api, in case that may be of interest

此外,在可能感兴趣的情况下,用于firebase api的ruby包装

#2


1  

I would say the easiest approach doing it would be using ActionController::Live. Using it you'll be able to send SSE(Server Sent Event) to your client while having js script there to catch and update your <%= @post.value %>. Here's a pretty nice manual on using it.

我会说最简单的方法就是使用ActionController :: Live。使用它,您将能够将SSE(服务器发送事件)发送到您的客户端,同时在那里使用js脚本来捕获并更新您的<%= @ post.value%>。这是一本非常好用的手册。

Still from my point of view the most appropriate way to implement things you want to do will be using something like Faye. It is a publish/subscribe messaging system which will allow you to update your client with new data as soon as it appears, e.g., you can set an after_save in your model and publish an update to all subscribed clients. And of course there is a gem also called faye to wrap its usage.

从我的观点来看,实现你想要做的事情最合适的方式就是使用像Faye这样的东西。它是一个发布/订阅消息传递系统,它允许您在新数据出现时立即更新您的客户端,例如,您可以在模型中设置after_save并向所有订阅的客户端发布更新。当然还有一个名为faye的宝石可以包装它的用法。

#1


2  

I would go with Firebase as opposed to polling the server.

我会使用Firebase而不是轮询服务器。

However, if you're wanting to poll the server periodically, I would just have a JavaScript method which executes every 10 seconds, or at whatever interval you'd like, and fetches whatever data you'd like asynchronously and subsequently updates the DOM.

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

Also, ruby wrapper for firebase api, in case that may be of interest

此外,在可能感兴趣的情况下,用于firebase api的ruby包装

#2


1  

I would say the easiest approach doing it would be using ActionController::Live. Using it you'll be able to send SSE(Server Sent Event) to your client while having js script there to catch and update your <%= @post.value %>. Here's a pretty nice manual on using it.

我会说最简单的方法就是使用ActionController :: Live。使用它,您将能够将SSE(服务器发送事件)发送到您的客户端,同时在那里使用js脚本来捕获并更新您的<%= @ post.value%>。这是一本非常好用的手册。

Still from my point of view the most appropriate way to implement things you want to do will be using something like Faye. It is a publish/subscribe messaging system which will allow you to update your client with new data as soon as it appears, e.g., you can set an after_save in your model and publish an update to all subscribed clients. And of course there is a gem also called faye to wrap its usage.

从我的观点来看,实现你想要做的事情最合适的方式就是使用像Faye这样的东西。它是一个发布/订阅消息传递系统,它允许您在新数据出现时立即更新您的客户端,例如,您可以在模型中设置after_save并向所有订阅的客户端发布更新。当然还有一个名为faye的宝石可以包装它的用法。