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.
** ① 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.