Reputation: 10422
I am trying to use distance_of_time_in_words
in rails 4. I have a datetime field in the db called expires_at
. I've tried a few different ways to format it but can't seem to get it to work properly.
<%= offer.expires_at.strftime('%I:%M %p %b %d') %> // works properly: 12:00 AM Mar 29
<%= distance_of_time_in_words(offer.expires_at) %> // fails: undefined method `to_datetime' for 0:Fixnum
<%= distance_of_time_in_words(Time.parse(offer.expires_at.to_s)) %> // fails as above
<%= distance_of_time_in_words(offer.expires_at.to_time.to_i) %> // works but shows '44 years' as distance
any pointers for a RoR noob?
Upvotes: 2
Views: 1373
Reputation: 10473
You want to pass in a second time parameter. Without the second parameter, it'll calculate it from 0
(or the Epoch -- This is why you got 44 years since the Epoch is often from January 1st, 1970). Try something like this:
<%= distance_of_time_in_words(offer.expires_at, Time.now) %>
This will calculate it from the time the offer expires to the current time. You can switch the parameter order if that makes more sense to you, but it'll output the same thing.
There's actually a shortcut for this:
<%= distance_of_time_in_words_to_now(offer.expires_at) %>
Upvotes: 6