Railsアプリを日本時間化する

「config/application.rb」に

「time_zone(L10)」「active_recordのtime_zone(L11)」にまつわる下2行を追記。
 これにより、アプリの中の時間とDBの時間がどちらも日本時間になる。

module ChatSpace
  class Application < Rails::Application
    config.generators do |g|
      g.stylesheets false
      g.javascripts false
      g.helper false
      g.test_framework false
    end
    config.i18n.default_locale = :ja
    config.time_zone
    config.active_record.default_timezone = :local
  end
end

ちなみに、(L9)はエラーメッセージの日本語化の指定の奴。


ビューの表示箇所では、以下のようにstrftimeメソッドで見た目を調整できる。

updated_at.strftime("%Y-%m-%d %H:%M")
→ 2018-11-27 04:54
updated_at.strftime("%Y年%m月%d日 %H時%M分")
→ 201811270454