gpt4 book ai didi

ruby-on-rails - Ruby on Rails - 使用国家选择器显示国家真实姓名

转载 作者:数据小太阳 更新时间:2023-10-29 07:55:47 25 4
gpt4 key购买 nike

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

gem 'countries'
gem 'country_select'
gem 'simple_form'

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

在用户的展示页面`

<%= @user.country %>` => Displays GB

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

最佳答案

来自 the countries gem's github page :

This gem automatically integrates with country_select. It will change its behavior to store the alpha2 country code instead of the country name.

然后,从country_selectgithub page

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

关于ruby-on-rails - Ruby on Rails - 使用国家选择器显示国家真实姓名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26867057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com