So, I downloaded and installed the ActiveHelper gem, but I still can't figure out how to use ActionView's DateHelper methods, such as time_ago_in_words, in normal Ruby code. Is this not included in ActiveHelper? Is it possible to use these methods outside of Rails?
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_ago_in_words
Try this:
require 'action_view'
require 'action_view/helpers'
include ActionView::Helpers::DateHelper
time_ago_in_words(Time.now - 60*60*2) + ' ago'
#=> "about 2 hours ago"
If you need to take advantage of Numeric ActiveSupport extensions:
require 'active_support/core_ext/numeric/time'
time_ago_in_words(Time.now - 2.hours) + ' ago'
#=> "about 2 hours ago"
# note that (Time.now - 2.hours) == (2.hours.ago)
Reference for Non Rails App : https://github.com/elgalu/time_ago_in_words#non-rails-apps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With