Вопрос

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] }
Это было полезно?

Решение

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] }

Другие советы

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"]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top