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 3 years have passed since last update.

複数テーブルに存在するカラムのi18n定義を共通化する

Last updated at Posted at 2020-02-20

複数テーブルに共通して存在するカラム(ex. created_at, updated_at)を各テーブルで定義するのは冗長でめんどくさい。

config/locales/translation_ja.yml

ja:
  activerecord:
    attributes:
      user:
        name: ユーザ名
        created_at: 登録日時
        updated_at: 更新日時
      event:
        name: イベント名
        created_at: 登録日時
        updated_at: 更新日時

こんな風に書かなくても、ja/activerecord/attributesではなく、ja直下のja/attributesで定義すれば使い回すことができる。

config/locales/translation_ja.yml

ja:
  attributes:
    created_at: 登録日時
    updated_at: 更新日時

<%= User.human_attribute_name(:created_at) %>

=> '登録日時'
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?