2

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!

Al17
  • 431
  • 7
  • 20

2 Answers2

0

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

jcgil
  • 1,542
  • 1
  • 13
  • 8
0

This worked really nicely for me (I had occasional nils and "").

In application_helper.rb:

def country_name(country_code)
  unless country_code.nil? || country_code == ""
    country = ISO3166::Country[country_code]
    country.translations[I18n.locale.to_s] || country.common_name || country.iso_short_name
  end
end

In the view

<%= country_name(@user.country_code) %>

It's based on the country_select docs.

Example usage

arr = ["AU", "US", nil, ""]
arr.map{ |country_code| country_name(country_code) }
# => ["Australia", "United States", nil, nil]
stevec
  • 41,291
  • 27
  • 223
  • 311