0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

rails twitterみたいに投稿してからの時間を表示 完全版

Posted at

rails でTwitterのように投稿時間を「何分前」と表示したい場合

ステップ1  posts controoller のcreateアクションでPost .newにcreated_at:        params[:created_at])を追加する

ステップ2  index.htmlで投稿一覧をつくる。(これは自分の作りたいものによって変わる)

ステップ3  表示したいview(私の場合index.html)で<%= time_ago_in_words(post.created_at) %>前 を書く。

ステップ4  about 1 hour 前 や  1day 前         などが表示されるようになる。

ステップ5  config.time_zone = "Asia/Tokyo"
        config.i18n.default_locale = :ja   をconfig/application.rbで入力する

ステップ6  config/locales/en.yml で 
ja:
datetime:
distance_in_words:
half_a_minute: "30秒前後"
less_than_x_seconds:
one: "1秒"
other: "%{count}秒"
x_seconds:
one: "1秒"
other: "%{count}秒"
less_than_x_minutes:
one: "1分"
other: "%{count}分"
x_minutes:
one: "約1分"
other: "%{count}分"
about_x_hours:
one: "約1時間"
other: "約%{count}時間"
x_days:
one: "1日"
other: "%{count}日"
about_x_months:
one: "約1ヶ月"
other: "約%{count}ヶ月"
x_months:
one: "1ヶ月"
other: "%{count}ヶ月"
almost_x_years:
one: "1年弱"
other: "%{count}年弱"
about_x_years:
one: "約1年"
other: "約%{count}年"
over_x_years:
one: "1年以上"
other: "%{count}年以上"
を入力する

ステップ6の内容を変えれば約を消せたりします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?