让我们一起来情景:
柜台10秒
在用户访问 show.html.erb
页
show.html.erb
获取从数据库
使用&LT的价值;%= @ post.value%>
计数器开始计数和每个迭代是10秒
。
在每个10秒
我想改变 @ post.value
使用工具后
类。
更新 @ post.value
在数据库中。
刷新 show.html.erb
自动<%= @ post.value%>
将显示更新的值
在上述过程将在循环中运行,直到用户移动到其他页面。
如果我必须简化code中的问题,那么这将是这样的:
<%= @ post.value%>
&其中;%= @ post.name%GT;
高清节目
@post = Post.find(PARAMS [:ID])
结束
DEF更新
....#这是空的,现在
结束
高清fetching_value_from_PostUpdate(current_value)
..#现在我需要更新数据库中的值
结束
我要更新 post.value
方法写在这个类的基础上。伪code:
类PostUpdate
@@ instance_variable
高清初始化(post_value)
@@ instance_variable = Marshal.load(Marshal.dump(post_value))
结束
#Method需要计算的值
高清update_data_in_database
...
返回数据
结束
问题
在哪里我必须把柜台?客户端或服务器端?我不想使用后台作业
的轨道。
在什么样的变化,我需要做的显示
方法,这样以后每间隔页面会自动刷新,并通过在 @post更新值。值
?
感谢您的帮助!
解决方案我将与火力地堡去,而不是到轮询服务器。
不过,如果你想定期轮询服务器,我只想有一个它执行每隔10秒,或在任何时间你想JavaScript方法,并获取任何数据,你想异步并随后更新DOM的。
另外,红宝石包装火力API 时,在的情况下可能会感兴趣
Let's take a scenario:
counter 10 seconds
User visited show.html.erb
page
show.html.erb
fetch the value from database
using <%= @post.value %>
.
Counter started and each iteration of counter is for 10 seconds
.
After every 10 seconds
I wanted to change the @post.value
using utility
class.
update the @post.value
in database.
Refresh the show.html.erb
automatically and <%= @post.value %>
will show the updated 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:
<%= @post.value %>
<%= @post.name %>
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
I want to update the post.value
on the basis of method written in this class. Pseudo code:
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 usebackground jobs
in 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
?
Thanks for your help!
解决方案I would go with Firebase as opposed to polling the server.
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.
Also, ruby wrapper for firebase api, in case that may be of interest