我试图在持续时间内获得比“大约2小时”更准确的输出。
当前,我们正在使用time_ago_in_words
这看起来像.erb中的以下功能
Duration: <%= time_ago_in_words(@sitrep.incident.created_at) %>
# I found the gem `dotiw` which lets me use:
distance_of_time_in_words(@sitrep.incident.created_at)
# However, there is an error and I can't get either to work in irb
这是我尝试两者时的控制台输出。感谢所有帮助
[1] pry(main)> include ActionView::Helpers
=> Object
[2] pry(main)> t = Sitrep.last.incident.created_at
# redacted output
=> Wed, 28 Nov 2018 21:47:45 UTC +00:00
[3] pry(main)> t
=> Wed, 28 Nov 2018 21:47:45 UTC +00:00
[4] pry(main)> time_ago_in_words(t)
=> "<span class=\"translation_missing\" title=\"translation missing: en.weeks\">Weeks</span>, <span class=\"translation_missing\" title=\"translation missing: en.days\">Days</span>, <span class=\"translation_missing\" title=\"translation missing: en.hours\">Hours</span>, and <span class=\"translation_missing\" title=\"translation missing: en.minutes\">Minutes</span>"
[5] pry(main)> distance_of_time_in_words(t)
NoMethodError: undefined method `seconds' for Wed, 28 Nov 2018 21:47:45 UTC +00:00:Time
Did you mean? send
Did you mean? send
from /Users/mmowris/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0/gems/activesupport-4.2.7.1/lib/active_support/time_with_zone.rb:371:in `method_missing'
[6] pry(main)>
第一种方法time_ago_in_words适用于我们的Rails应用程序。该.erb文件发送一封电子邮件,其输出类似于:
Duration: about 2 hours
关于如何进行的任何想法?为什么这在我们的rails项目中有效,但在irb中无效?为什么不能让dotiw使用相同的输入?为什么我不能让taiw在生产中使用相同的输入?
谢谢!
答案 0 :(得分:1)
使用帮助程序,而不是手动添加帮助程序
> helper.time_ago_in_words(2.hours.ago, true)
=> "2 hours"
> helper.time_ago_in_words((2.hours.ago + 5.seconds), true)
=> "1 hour, 59 minutes, and 55 seconds"