LoginSignup
1
2

More than 5 years have passed since last update.

localesで設定した名称にアクセスする方法いくつか

Posted at

よく忘れがちで検索してるのでメモ代わりに。

ActiveRecordのクラス、Recordに対し、
configs/locales/ja.yml で

ja:
  activerecord:
    models:
      record: レコード
    attributes:
      record:
        name: 名前

と指定していた場合

Viewのフォームでラベルとして利用する

@record = Record.new
の環境下で

<%= label :record, :name %>
<%= text_field :record, :name %>

とすると、label :record, :name が[名前]を出してくれる。(タグ付きです)

ActiveRecordのクラスにつけた名称にアクセスしたい

Record.model_name.human

ラベルを経由せずに項目名を取得したい

Record.human_attribute_name("name")

1
2
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
1
2