获取对象属性Rails

时间:2014-02-25 20:37:30

标签: ruby-on-rails ruby rails-geocoder

我正在尝试使用带有geocoder gem的邮政编码获取城市,我的工作就像我得到这样的对象:

---
      - !ruby/object:Geocoder::Result::Google
        data:
          address_components:
          - long_name: AB10 1AA
            short_name: AB10 1AA
            types:
            - postal_code
          - long_name: Aberdeen
            short_name: Aberdeen
            types:
            - locality
            - political
          - long_name: Aberdeen City
            short_name: Aberdeen City
            types:
            - administrative_area_level_2
            - political
          - long_name: United Kingdom
            short_name: GB
            types:
            - country
            - political
          - long_name: Aberdeen
            short_name: Aberdeen
            types:
            - postal_town
          formatted_address: Aberdeen, Aberdeen City AB10 1AA, UK
          geometry:
            bounds:
              northeast:
                lat: 57.1487081
                lng: -2.0960462
              southwest:
                lat: 57.1478514
                lng: -2.0972085
            location:
              lat: 57.1482317
              lng: -2.0966478
            location_type: APPROXIMATE
            viewport:
              northeast:
                lat: 57.1496287302915
                lng: -2.095278369708498
              southwest:
                lat: 57.1469307697085
                lng: -2.097976330291502
          types:
          - postal_code
        cache_hit: 

现在我有获得城市的问题......据我所知,它是对象所以(让我们称之为位置)location.data.address_components.long_name但我得到的错误如undefined method数据'为#`..我做错了什么?

提前致谢!

1 个答案:

答案 0 :(得分:0)

Ouhh ......这篇文章CLICK帮助了我!解决方案很简单:location[0].city