开发者

Rails "moments ago" instead of created date?

开发者 https://www.devze.com 2023-01-14 05:03 出处:网络
Currently I have the following in my helper: def created_today k if k.created_at.to_date == Date.today then

Currently I have the following in my helper:

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

What I would like to do is replace TODAY with MOMENTS AGO if the created_at time is within the last 10 minutes.

Is this possible?

Would I just add another 'i开发者_如何学Pythonf' line after the current 'if k.created_at.to_date == Date.today then'?

Thanks,

Danny

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


You could do

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

Note that you have to put the 10-minute-check before the today-check because a date less than 10 minutes ago is most likely on the same day.

0

精彩评论

暂无评论...
验证码 换一张
取 消