我正在尝试向用户显示问候语。到目前为止,我有:
def greet
now = Time.now
today = Date.today.to_time
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'
end
end
在我看来,我试图致电<%= greet %>
但是,当我这样做时,我收到错误
undefined local variable or method `greet'
我不确定如何显示我的信息
<%= greet %>, <%= current_user.username %>
答案 0 :(得分:2)
如果您希望它在HTML模板中可用,则应将其添加到帮助程序中。由于这是一种通用的方法,我可能会建议app/helpers/application_helper.rb
文件。这应该使它可用于模板。