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)
我的问题是,如何将英国显示为全名?
该 gem 自动与country_select 集成。它将更改其行为以存储 alpha2 国家/地区代码而不是国家/地区名称。
然后,来自country_select的github 页面
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)
| 归档时间: |
|
| 查看次数: |
4358 次 |
| 最近记录: |