almost 4 years ago

系統:

Mac OS X Yosemite 10.10.1
Ruby 2.1.5
Rails 4.1.8

相關連結

Timeago ( http://timeago.yarp.com/ )
GitHub ( https://github.com/rmm5t/jquery-timeago )

設定步驟如下

1. 下載檔案

app/assets/javascripts/jquery.timeago.js
app/assets/javascripts/jquery.timeago.zh-TW.js

2. 呼叫函式

app/assets/javascripts/base.js.coffee

$ ->
  # timeago
  $('abbr.timeago').timeago();

3. 新增 Helper

app/helpers/utility_helper.rb

module UtilityHelper
  def timeago(time, options = {})
    options[:class] ||= 'timeago'
    content_tag(:abbr, time.to_s, options.merge(title: time.getutc.iso8601)) if time
  end
end

4. 在 views 使用方法

%p= timeago('2014-11-21 09:38:27.256503'.to_time)
← OS X Yosemite 10.10.1 安裝設定網頁伺服器 Pow Devise - part 1 - 基本安裝 →
 
comments powered by Disqus