views:

22

answers:

3

Hi Everyone,

A follow on from this questions:

http://stackoverflow.com/questions/3032598/rails-created-at-on-display-if-today

Is it possible to output the word TODAY rather than the date when using the following helper?

def created_today k
   k.created_at if k.created_at.to_date == Date.today
end

<%=h created_today(k) %>

Thanks,

Danny

A: 
def created_today k
  'TODAY' if k.created_at.to_date == Date.today
end

<%=h created_today(k) %>
mark
That doesn't seem to work, should I be able to put anything in place of DATE?Currently, using your suggestion, I just get an empty span.
dannymcc
try it with puts 'TODAY' ...
mark
Thanks for helping Mark, it seems replacing puts 'TODAY' with just "TODAY" works a charm. Thanks again.
dannymcc
+1  A: 
def created_today k 
   "Today" if k.created_at.to_date == Date.today 
end
j.
Worked a treat, I didn't realise you could just replace the string with anything you like! Thanks...again!
dannymcc
You're welcome...
j.
+1  A: 

If you want to display the date if it's not today:

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

In your css, you describe how you want to 'highlight' it

True Soft
Thats a nice idea, is it possible to give the two different styles? So if it was today I could style it to be highlighted?
dannymcc
Great, thats really brilliant. Thanks!
dannymcc