enumで取得したデータを他のビューファイルで表示できない(日本語化にならない)

実現したいこと

ここに実現したいことを箇条書きで書いてください。

前提

親モデル(post)にcountry/whiskey_typeのenumを定義し、セレクトボックスの実装を終え、正常に日本語化までできているのですが、データ保存後の詳細画面上では、
なぜか日本語化されず英語での表示になってしまいます。

https://haayaaa.hatenablog.com/entry/2019/02/20/212805

上記の記事を参考にして、
show.html.erbを、<%= country.name %> => <%= country.name_i18n %>
に変更したところ、下記のようなエラーになってしまいます。

どなた様か原因がわかる方がいましたら、ご教示ただければと思います。

発生している問題・エラーメッセージ

undefined method `name_i18n' for #<Country id: 4, name: "scotland", post_id: 4, created_at: "2023-02-28 13:42:00.487629000 +0900", updated_at: "2023-02-28 13:42:00.487629000 +0900"> Did you mean? name_change

Image from Gyazo

該当のソースコード

app/views/posts/show.html.erb

1一部抜粋 2 <div class="col-lg-6"> 3 <div class="card mb-3 mb-lg-0"> 4 <div class="card-body"> 5 <h3 class="my-3"><%= @post.title %></h3> 6 <h5 class="my-3"><%= @post.body %></h5> 7 <hr> 8 <h3 class="my-3"><%= t('.category')%></h3> 9 10 <p class="bi bi-trash2-fill"> <%= @post.whiskey_brand %></p> 11 12 <% @post.whiskey_types.each do |whiskey_type| %> 13 <p class="bi bi-bricks"> <%= whiskey_type.name %></p> 14 <% end %> 15 16 <% @post.countries.each do |country| %> 17 <p class="bi bi-globe-asia-australia"> <%= country.name_i18n %></p> 18 <% end %> 19 20 <i class="bi bi-cash-coin"> <%= @post.price %></i> 21 <%= render 'crud_menus', post: @post if current_user.own?(@post) %> 22 </div> 23 </div> 24 </div> 25一部抜粋

app/views/posts/_formhtml.erb

1<%= form_with model: post, local: true do |f| %> 2 <%= render 'shared/error_messages', object: f.object %> 3 <div class="form-group"> 4 <%= f.label :title %> 5 <%= f.text_field :title, class: 'form-control' %> 6 </div> 7 <div class="form-group"> 8 <%= f.label :body %> 9 <%= f.text_area :body, class: 'form-control', rows: 10 %> 10 </div> 11 <div class="form-group"> 12 <%= f.label :whiskey_brand %> 13 <%= f.text_field :whiskey_brand, class: 'form-control' %> 14 </div> 15 <div class="form-group"> 16 <%= f.fields_for :countries, post.countries do |country| %> 17 <%= country.label :name %> 18 <%= country.select :name, Post.countries_i18n.invert, {}, class: 'form-control' %> 19 <% end %> 20 </div> 21 <div class="form-group"> 22 <%= f.fields_for :whiskey_types, post.whiskey_types do |whiskey_type| %> 23 <%= whiskey_type.label :name %> 24 <%= whiskey_type.text_field :name, class: 'form-control' %> 25 <% end %> 26 </div> 27 <div class="form-group"> 28 <%= f.label :price %> 29 <%= f.text_field :price, class: 'form-control' %> 30 </div> 31 <div class="form-group"> 32 <%= f.label :post_image %> 33 <%= f.file_field :post_image, class: 'form-control mb-3', accept: 'image/*' %> 34 <%= f.hidden_field :post_image_cache %> 35 </div> 36 <%= f.submit class: 'btn btn-primary' %> 37<% end %>

app/models/post.rb

1enum country: { scotland: 0, ireland: 1, america: 2, canada: 3, japan: 4, taiwan: 5, india: 6, others: 7 }, _prefix: true 2enum whiskey_type: { malt: 0, grain: 1, belended: 2, bourbon: 3, rye: 4, corn: 5, others: 6 }, _prefix: true

config/locals/enums.ja.yml

1ja: 2 enums: 3 post: 4 country: 5 scotland: 'スコットランド' 6 ireland: 'アイルランド' 7 america: 'アメリカ' 8 canada: 'カナダ' 9 japan: '日本' 10 taiwan: '台湾' 11 india: 'インド' 12 others: 'その他' 13 whikey_type: 14 malt: 'モルト' 15 grain: 'グレーン' 16 belended: 'ブレンデッド' 17 bourbon: 'バーボン' 18 rye: 'ライ' 19 corn: 'コーン' 20 others: 'その他'

試したこと

<%= country.name %>の入力にすると下記のように国名が英語表記となってしまう。

Image from Gyazo

<%= country.name_i18n %> と記述するとエラーになってしまう。

希望は、ここの国名が日本語化されて表示されて欲しいです。

補足情報(FW/ツールのバージョンなど)

ruby:3.1.3
rails:6.1.7

コメントを投稿

0 コメント