Ruby on Rails- 使用国家/地区选择器显示国家/地区真实姓名

Mar*_*ith 4 ruby ruby-on-rails country-codes simple-form ruby-on-rails-4

在我的 Rails 应用程序中,我安装了以下 gem

gem 'countries'
gem 'country_select'
gem 'simple_form'
Run Code Online (Sandbox Code Playgroud)

当用户注册时,他们选择一个国家/地区(例如英国)

在用户的显示页面上 `

<%= @user.country %>` => Displays GB
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何将英国显示为全名?

seb*_*kom 5

来自国家 gem 的 github 页面

该 gem 自动与country_select 集成。它将更改其行为以存储 alpha2 国家/地区代码而不是国家/地区名称。

然后,来自country_selectgithub 页面

class User < ActiveRecord::Base
  # Assuming country_select is used with User attribute `country_code`
  # This will attempt to translate the country name and use the default
  # (usually English) name if no translation is available

  def country_name
    country = ISO3166::Country[country_code]
    country.translations[I18n.locale.to_s] || country.name
  end
end
Run Code Online (Sandbox Code Playgroud)