问题
I use simple_form
gem to select country:
= simple_form_for @shop, :url => { :action => "create" }, :html => {:id => 'new_shop' } do |f|
= f.simple_fields_for :address, :html => {:multipart => true} do |o|
= o.input :country, :label => "Country"
But the name of the country is being saved in short format in the database (like RU
, FR
, AU
, and so on).
I wonder, how can I show the full, long name of the country in the views? Thanks!
回答1:
Actually is a good idea saving the country code in the database (not the long name) because I18n. Having the code you can get later the name as follows:
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
Check: country_select: Getting the Country Name from the countries gem
来源:https://stackoverflow.com/questions/25908128/rails-simple-form-how-to-show-long-name-of-the-country