2010-09-01 17 views
0

目前,我在我的幫助下:Rails「瞬間之前」而不是創建日期?

def created_today k 
    if k.created_at.to_date == Date.today then 
    content_tag(:span, 'Today', :class => "todayhighlight") 
    else 
    k.created_at.to_s(:kasecreated) 
    end 
end 

我想這樣做是AGO MOMENTS如果created_at時間是在最後10分鐘內更換TODAY。

這可能嗎?

我會在當前'if k.created_at.to_date == Date.today then'之後添加另一個'if'行嗎?

感謝,

丹尼

UPDATE:

def created_today k 
    if k.created_at.to_date == Date.today then 
    content_tag(:span, 'Today', :class => "todayhighlight") 

    if k.created_at > 1.minutes.ago then 
    content_tag(:span, 'Moments Ago', :class => "todayhighlight") 

    else 
    k.created_at.to_s(:kasecreated) 
    end 
end 

回答

1

你可以做

if k.created_at > 10.minutes.ago then 
    ... 
elsif k.created_at.to_date == Date.today then 
    ... 
else 
    ... 

請注意,您必須把TODAY-前10分鐘檢查請檢查,因爲不到10分鐘前的日期很可能在同一天。

+0

嗨斯文,這工作,以取代當前的if語句。我如何組合多個?查看更新的問題。 – dannymcc 2010-09-01 14:14:40

+0

更新了我的答案。現在清楚嗎? – 2010-09-01 14:15:32

+0

工作過的魅力,謝謝! – dannymcc 2010-09-01 14:20:01

相關問題