Question

I am using country gem https://github.com/hexorx/countries and trying to get the the country name from alpha2 country names. but it comes as [object object]. here is my code.

render :json => @countries.map { |c| [c.id, ::ISO3166::Country[c.country]] }

This returns aplha2 fine as expected, which is saved in country column:

render :json => @countries.map { |c| [c.id, c.country] }
Was it helpful?

Solution

You need to pass hash (data) instead of Country instance.

render :json => @countries.map { |c| [c.id, ::ISO3166::Country[c.country].data] }

If you want only country name, use name:

render :json => @countries.map { |c| [c.id, ::ISO3166::Country[c.country].name] }

OTHER TIPS

Another option is accessing the translations, because for example sometimes the country name is not what you want e.g.: from "United States of America" to "United States"

country_code = "US"
country_label = ::ISO3166::Country[country_code].data["translations"]["en"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top