I am doing tutorial from jumpstartlab.com called "Blogger2" . There is a part where they suggest to add timestamp for the comment section.
Add a Timestamp to the Comment Display
We should add something about when the comment was posted. Rails has a really neat helper named >
distance_of_time_in_words
which takes two dates and creates a text description of their >difference like "32 minutes later", "3 months later", and so on.You can use it in your
_comment.html.erb
partial like this:
<p>Posted <%= distance_of_time_in_words(comment.article.created_at, comment.created_at) %> later</p>
And now, when I add comments to my articles those timestamps are showing in my opinion just some random values. Immediately after adding comment there is like "Posted about 3 hours later". Any idea how to fix that?
Image of few random comments with few random timestamps.
Thanks!
It's correct.
If the article was created three hours ago, and you add a comment now, the comment is "about three hours later"... about three hours after the article was created.
If you want to know how long ago the comment was posted from now, you need a different helper, time_ago_in_words
Posted <%= time_ago_in_words(comment.created_at) %> ago.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.