[英]link that changes every 24 hours?
因此,我有一個結構像一本書的Rails應用程序。 然后是一個章節模型。 用戶每天需要閱讀一章。
我有一個章節索引頁面,但是我想在應用程序的儀表板中創建一個按鈕,將用戶直接重定向到當天的章節,從而為用戶節省時間。
因此,我需要在按鈕中創建一個鏈接,該鏈接每24小時切換到下一章。 如何創建一個每24小時更改為下一個數據庫記錄的鏈接?
確切答案取決於您的實現。 但是,這里有一些提示:
如果您將開始日期存儲在某處,則可以使用以下類似方法輕松獲取兩個日期之間的天數:
today = Date.today start_date = Book.start_date # this will depend on implementation @current_chapter = today - start_date + 1 # we add 1 because the first chapter is 1, not 0
那么您的視圖可以執行<%= link_to "Go to today's chapter", controller: :chapter, action: :show, id: @current_chapter %>
如果每個人都同時開始讀這本書,則可以將其存儲在book對象上
UserBook
)。 在這種情況下,您可能應該在此處創建start_date
字段。 我會使用Date
,因為它每天都在變化。
您為第一章保存了date
,為便於說明,請說是
a = Date.new(2016, 1, 1) # January 1st 2016
b = Date.today # every day it is diferent
# b = February 4th 2016
c = (b - a).to_i # c = 34
# 34 days have passed since January 1st
# tomorow it will be 35
您可以在鏈接中使用c
作為記錄,例如
@chapter = Chapter.find(c)
link_to "Chapter #{c}", chapter_path(@chapter)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.