Ruegen
Ruegen

Reputation: 665

country_select display full country name on show action

I use the gem 'country_select' to choose a country for a form on my rails app however when I go to a show or index action I see the stored value. Is there a method to convert the value to a full name string of the country?

[AU] => "Australia"

Upvotes: 0

Views: 2275

Answers (2)

stevec
stevec

Reputation: 52318

Based on the country_select documentation I place this 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

and use it in the view like so:

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

Upvotes: 0

Kristj&#225;n
Kristj&#225;n

Reputation: 18813

The documentation includes how to get exactly what you want.

country = ISO3166::Country[country_code]
country.translations[I18n.locale.to_s] || country.name

Upvotes: 8

Related Questions