简体   繁体   English

Geokit-rails插件-无法在返回的GeoLoc对象上调用to_json,给出循环引用错误

[英]Geokit-rails plugin - can't call to_json on returned GeoLoc object, gives circular reference error

I was using an earlier version of geokit plugin on a rails 2.3 app and just updated to this latest version - the one that now uses a gem. 我在Rails 2.3应用程序上使用的是geokit插件的较早版本,并且刚刚更新为最新版本-现在使用的是gem。

In the previous version it was possible to convert the object returned by the .geocode method to json. 在以前的版本中,可以将.geocode方法返回的对象转换为json。 by just calling the to_json method on it. 只需调用to_json方法即可。

This however breaks in the new version. 但是,这在新版本中被破坏。

I noticed there is an attribute @all in the new GeoLoc which produces a circular reference error if you call to_json on it. 我注意到新的GeoLoc中有一个属性@all,如果您调用to_json会产生循环引用错误。

Here is an example of the object returned: 这是返回的对象的示例:

<Geokit::GeoLoc:0x3837be4 @full_address="Phoenix, AZ, USA", @city="Phoenix", @provider="google", @country_code="US", @accuracy=4, @street_address=nil, @lng=-112.073821, @precision="city", @zip=nil, @success=true, @all=[#<Geokit::GeoLoc:0x3837be4...>], @lat=33.448263, @state="AZ">

And here is the error if you call to_json on it: 如果在上面调用to_json,则会出现错误:

ActiveSupport::JSON::CircularReferenceError (object references itself) ActiveSupport :: JSON :: CircularReferenceError(对象引用自身)

I am using the "json" to plot markers on the map via jquery. 我正在使用“ json”通过jquery在地图上绘制标记。 Of course, I can make my own string from the GeoLoc result and convert that to json - but it is more code... 当然,我可以从GeoLoc结果中创建自己的字符串,并将其转换为json-但这是更多代码...

Anyone know why that attribute has been added in the new version and whether there is another way to directly convert the geoLoc result to json? 有谁知道为什么在新版本中添加了该属性,以及是否存在另一种直接将geoLoc结果转换为json的方法?

Hey there -- the easiest way right now is to call my_geoloc.to_hash.to_json 嘿-现在最简单的方法是调用my_geoloc.to_hash.to_json

The circular ref happens because geocoding calls can return multiple results, which are kept in the @all attribute. 之所以发生循环引用,是因为地理编码调用可以返回多个结果,这些结果保存在@all属性中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM