Rails Simple_Form: How to show long name of the country

冷暖自知 提交于 2019-12-11 08:11:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!