Delayed_job 是您正在寻找的。
例如,您可以拥有一个遍历所有订单对象的函数。
def purge_orders
# purge_at is an attribute of order that contains
# the datetime when the order should be deleted.
Order.where("purge_at >= #{DateTime.current}").each {|order| order.destroy }
end
第二个问题更像是一个javascript问题。这个想法是,Rails 将剩余时间传递给客户端,然后 javascript 根据 Rails 端给出的剩余时间创建一个计时器。将 javascript 放入您的视图文件中。理想情况下,您应该在资产文件夹中隔离 javascript,但我不会在这里为您这样做。
<script language="JavaScript">
TargetDate = <%= @order.purge_at - DateTime.current %>;
BackColor = "palegreen";
ForeColor = "navy";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is finally here!";
</script>
<script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>
原始 javascript 代码的来源