0
现在我有一个函数time_since如何显示在Rails中更新的时间?
def time_since
seconds = Time.now - self.updated_at
if seconds < 15
display = "Just Now"
elsif seconds < 59.5 #less than 59.5 seconds
display = seconds.round(0).to_s + "s"
elsif seconds < (3600-30) #less than 59.5 minutes
display = (seconds/60).round(0).to_s + "m"
elsif seconds < (86400-1800) #less than 23.5 hours
display = (seconds/3600).round(0).to_s + "h"
elsif seconds < 86400*2 #less than 2 days
display = "Yesterday"
elsif seconds < 86400*7 #less than 1 week
display = self.updated_at.strftime("%A")
else #more than 1 week
mon = self.updated_at.month
day = self.updated_at.day.to_s
display = day + convert_month_number_to_month_name(mon).to_s
end
return display
end
而且我希望能够悬停在这一点,并把它显示确切的updated_at时间戳。有没有我可以轻松使用的宝石来做到这一点?
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words –
@anonymousxxx连接的东西很好,这基本上是你已经在那里实施。但更重要的是,你的问题是如何显示一个文本,但添加悬停文本?有一些宝石/库可以为它提供奇特的工具,但是你也可以查看一些基本的javascript来获得悬停事件,或者使用一个'title',这个'title'通常显示在网页浏览器上。 – MrDanA
这一切都很棒,但现在我得到一个错误:未定义的方法'distance_of_time_in_words'。我是否需要包含新的东西来访问该方法 – GoodLuckGanesh