如何在 rails 中的特定出版物之间创建时间间隔?
How to create time intervals between specific publications in rails?
我尝试构建一个食谱网络应用程序。这是一种“游戏”。每个用户都有可能喜欢一个食谱。起初,这个似乎是空的。然后,用户应该每隔一小时(或半小时)收到食谱的每一步。
目前,我有这些模型:
class Recipe < ApplicationRecord
belongs_to :user
has_many :steps
has_many :favorites
has_many :favorited_by_users, :through => :favorites, :source => 'user'
class Step < ApplicationRecord
belongs_to :recipe
end
class Favorite < ApplicationRecord
belongs_to :recipe
belongs_to :user
end
class User < ApplicationRecord
has_many :recipes
has_many :favorites
has_many :favorite_recipes, :through => :favorites, :source => 'recipe’
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
在我的步进控制器中我有:
def index
@recipe = Recipe.find(params[:recipe_id])
@steps = @recipe.steps
end
我想在我的索引步骤视图中显示:
用户喜欢的食谱的每一步,但有延迟(每次发布之间有 100 万或 1000 万,无论如何……可配置的东西)。是否可以隐藏步骤的名称,并在单击它后显示它?
#views/steps/index.html.erb
<h2><%=Recipe.name%></h2>
<% @steps.each do |step| %>
<p> <%=step.name%></p>
#30mn later… the second step
<p> <%=step.name%></p>
#30mn later… the third one…
<p> <%=step.name%></p>
等等……
这怎么可能?我应该使用 cron 还是 Active Job ?但是我该怎么办?
提前非常感谢! ;)
cron
或 Active Job
绝对不是您要查找的内容。此类工具用于服务器处理,而您在任务中需要客户端处理。您真正需要做的只是发送一个 ajax 请求,每 1000 万使用 javascript 向服务器发送您需要的步数。
请记住,您需要确保步骤请求的安全,并在用户完成最后一步时将时间存储在服务器端。如果您较早收到请求,那么您计划应该拒绝它(您需要这样的保护,因为技术用户可以研究您的逻辑并尝试手动发送请求以检索进一步的说明。
你的 JS 代码怎么可能是这样的:
setInterval(10000, function loadStep() {
ajax.get([url], function(stepData) {
// Some logic to display your step on the page
})
})
不要忘记设置正确的路由,这将在服务器端接受此类请求
您可以呈现 DOM 中的所有步骤,然后执行 javascript 以显示您的步骤,但这意味着每次您的页面重新加载时,您都将再次从步骤 1 开始。
如果你想保留进度,你需要将进度存储在 table 引用配方和用户的地方。
之后就是选择您的规则来检查进度、常规 Ajax 查询以注册新显示的步骤等。
我尝试构建一个食谱网络应用程序。这是一种“游戏”。每个用户都有可能喜欢一个食谱。起初,这个似乎是空的。然后,用户应该每隔一小时(或半小时)收到食谱的每一步。
目前,我有这些模型:
class Recipe < ApplicationRecord
belongs_to :user
has_many :steps
has_many :favorites
has_many :favorited_by_users, :through => :favorites, :source => 'user'
class Step < ApplicationRecord
belongs_to :recipe
end
class Favorite < ApplicationRecord
belongs_to :recipe
belongs_to :user
end
class User < ApplicationRecord
has_many :recipes
has_many :favorites
has_many :favorite_recipes, :through => :favorites, :source => 'recipe’
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
在我的步进控制器中我有:
def index
@recipe = Recipe.find(params[:recipe_id])
@steps = @recipe.steps
end
我想在我的索引步骤视图中显示:
用户喜欢的食谱的每一步,但有延迟(每次发布之间有 100 万或 1000 万,无论如何……可配置的东西)。是否可以隐藏步骤的名称,并在单击它后显示它?
#views/steps/index.html.erb
<h2><%=Recipe.name%></h2>
<% @steps.each do |step| %>
<p> <%=step.name%></p>
#30mn later… the second step
<p> <%=step.name%></p>
#30mn later… the third one…
<p> <%=step.name%></p>
等等……
这怎么可能?我应该使用 cron 还是 Active Job ?但是我该怎么办?
提前非常感谢! ;)
cron
或 Active Job
绝对不是您要查找的内容。此类工具用于服务器处理,而您在任务中需要客户端处理。您真正需要做的只是发送一个 ajax 请求,每 1000 万使用 javascript 向服务器发送您需要的步数。
请记住,您需要确保步骤请求的安全,并在用户完成最后一步时将时间存储在服务器端。如果您较早收到请求,那么您计划应该拒绝它(您需要这样的保护,因为技术用户可以研究您的逻辑并尝试手动发送请求以检索进一步的说明。
你的 JS 代码怎么可能是这样的:
setInterval(10000, function loadStep() {
ajax.get([url], function(stepData) {
// Some logic to display your step on the page
})
})
不要忘记设置正确的路由,这将在服务器端接受此类请求
您可以呈现 DOM 中的所有步骤,然后执行 javascript 以显示您的步骤,但这意味着每次您的页面重新加载时,您都将再次从步骤 1 开始。
如果你想保留进度,你需要将进度存储在 table 引用配方和用户的地方。
之后就是选择您的规则来检查进度、常规 Ajax 查询以注册新显示的步骤等。