如何在Rails中显示更新时间?

时间:2013-06-27 15:58:58

标签: ruby-on-rails timestamp

现在我有一个函数time_since

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时间戳。有没有我可以轻松使用的宝石来做这件事?

1 个答案:

答案 0 :(得分:0)

确定!我最终使用了这个:

<span title=<%= Object.updated_at.localtime.asctime.split.join('-') %>>
     <%= distance_of_time_in_words_to_now(Object.updated_at, include_seconds = true) %>
</span>

我无法获得超过当时第一个单词的标题,所以我加入破折号(即使我使用strftime)。这是功能性的,我稍后会清理它