Stuart Pontins Stuart Pontins - 5 months ago 3x
Ruby Question

How to make a time-based greeting message

I'm trying to display a greeting message to users. So far I have:

def greet
now =
today =

morning = today.beginning_of_day
noon = today.noon
evening = today.change( hour: 17 )
night = today.change( hour: 20 )
tomorrow = today.tomorrow

if (morning..noon).cover? now
'Good Morning'
elsif (noon..evening).cover? now
'Good Afternoon'
elsif (evening..night).cover? now
'Good Evening'
elsif (night..tomorrow).cover? now
'Good Night'

In my view I am trying to call
<%= greet %>
however, when I do this, I get an error

undefined local variable or method `greet'

I'm not sure how to display my message

<%= greet %>, <%= current_user.username %>


If you want it to be available in your HTML templates, you should add it to a helper. Since it's such a generic method, I might suggest the app/helpers/application_helper.rb file. This should make it available to the template.