我正在使用country gem https://github.com/hexorx/countries,并试图从alpha2国家名中获取国家名称。但它是作为物体来的。这是我的密码。
render :json => @countries.map { |c| [c.id, ::ISO3166::Country[c.country]] }这将像预期一样返回aplha2精细,它保存在country列中:
render :json => @countries.map { |c| [c.id, c.country] }发布于 2014-02-09 08:26:06
您需要传递散列(data)而不是Country实例。
render :json => @countries.map { |c| [c.id, ::ISO3166::Country[c.country].data] }如果只想要国名,请使用name
render :json => @countries.map { |c| [c.id, ::ISO3166::Country[c.country].name] }发布于 2021-11-03 15:40:08
另一种选择是访问翻译,因为例如,有时国家名称不是您想要的,例如:从“美利坚合众国”到“美国”
country_code = "US"
country_label = ::ISO3166::Country[country_code].data["translations"]["en"]https://stackoverflow.com/questions/21656758
复制相似问题