[Rails] I want to display "XX minutes ago" using created_at!

Overview

In this article, when displaying the creation date and time with Rails created_at, it describes how to display it as "XX minutes ago" instead of the conventional time notation. The completed image is as follows. スクリーンショット 2021-01-12 22.01.28.png

Development environment

procedure

** ① Set Japanese **

First, set Japanese. Write the following in the application.rb file of config

config/application.rb


config.i18n.default_locale = :ja

** ② Create a Japanese yaml file **

Next, write the settings in the yaml file as to what kind of Japanese notation to use. Ja.yml related to Japanese is not included in Rails by default, so please create it referring to the following. Create the ja.yml file under config/locales.

config/locales/ja.yml


ja:
  datetime:
    distance_in_words:
      about_x_hours:
        one:About 1 hour
        other:about%{count}time
      about_x_months:
        one:About 1 month
        other:about%{count}months
      about_x_years:
        one:About 1 year
        other:about%{count}Year
      almost_x_years:
        one:Less than a year
        other: "%{count}Less than a year"
      half_a_minute:Around 30 seconds
      less_than_x_seconds:
        one:Within 1 second
        other: "%{count}Less than a second"
      less_than_x_minutes:
        one:Within 1 minute
        other: "%{count}Less than a minute"
      over_x_years:
        one:over 1 year
        other: "%{count}Over a year"
      x_seconds:
        one:1 second
        other: "%{count}Seconds"
      x_minutes:
        one:1 minute
        other: "%{count}Minutes"
      x_days:
        one:1 day
        other: "%{count}Day"
      x_months:
        one:1 month
        other: "%{count}months"
      x_years:
        one:1 year
        other: "%{count}Year"

** ③ Display in view **

Finally, it is displayed in the view. To display it, use the time_ago_in_words method provided by Rails. Pass the creation date and time as an argument.

erb:index.html.erb


<p>#{time_ago_in_words(post.created_at)}Output before</p>

* Please rewrite the post part as appropriate.

This completes the implementation! If you want to modify the display method to your liking, please modify it in `ja.yml`.

Recommended Posts

[Rails] I want to display "XX minutes ago" using created_at!
I want to display the number of orders for today using datetime.
I want to play with Firestore from Rails
[Rails] I want to load CSS with webpacker
I want to be able to read a file using refile with administrate [rails6]
I want to judge the range using the monthly degree
I want to use a little icon in Rails
I want to display the name of the poster of the comment
I want to authenticate users to Rails with Devise + OmniAuth
I want to define a function in Rails Console
I want to call the main method using reflection
[Rails] I want to display the link destination of link_to in a separate tab
[Rails] [bootstrap] I want to change the font size responsively
I want to convert characters ...
I want to build Java Applet without using an IDE
I want to push an app made with Rails 6 to GitHub
What I was addicted to while using rspec on rails
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I want to create a form to select the [Rails] category
I want to narrow down the display of docker ps
[Rails] I tried to implement "Like function" using rails and js
I want to use FireBase to display a timeline like Twitter
I want to convert an array to Active Record Relation with Rails
I just want to write Java using Eclipse on my Mac
I tried to build a simple application using Dockder + Rails Scaffold
I want to display the images under assets/images in the production environment
I want to add devise in Rails, but I can't bundle install
I tried to display the calendar on the Eclipse console using Java.
[Rails] I want to add data to Params when transitioning with link_to
[Rails 5] How to display the password change screen when using devise
[rails] How to display db information
I want to use FormObject well
I want to convert InputStream to String
[Rails] Reflection to db using seeds.rb
I want to display images with REST Controller of Java and Spring!
[Note] I want to get in reverse order using afterLast with JdbcTemplate
[Ruby] I want to display posted items in order of newest date
I want to display a PDF in Chinese (Korean) with thin reports
I want to display an error message when registering in the database
I want to introduce the committee with Rails without getting too dirty
I want to develop a web application!
I want to write a nice build.gradle
I tried to introduce CircleCI 2.0 to Rails app
I want to eliminate duplicate error messages
[Rails] How to upload images using Carrierwave
I want to make an ios.android app
I want to use DBViewer with Eclipse 2018-12! !!
I want to RSpec even at Jest!
I want to write a unit test!
[Rails] How to display error messages individually
I want to install PHP 7.2 on Ubuntu 20.04.
Basic Rails commands you want to learn
I want to stop Java updates altogether
I want to use @Autowired in Servlet
How to implement image posting using rails
I want to target static fields to @Autowired
I want to do team development remotely
[Rails] How to use video_tag to display videos
[Rails] How to handle data using enum
[Rails / ActiveRecord] I want to validate the value before the type is converted (_before_type_cast)
Development memo ~ I want to display only the first image posted multiple times ~