Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.1

Rails Alpha2 country to Real country name


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

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