【问题标题】:Google Maps integration with JSON - CircularReferenceError谷歌地图与 JSON 的集成 - CircularReferenceError
【发布时间】:2010-04-03 06:44:37
【问题描述】:

更新二

我想通了。使用 google maps api 时,模型不得命名为“maps”。 to_json 在我创建新模型名称后起作用。

更新

@maps 是我的模型的名称,它包含地址、经度、纬度等信息。当我替换 <%= @maps.to_json %>; to <%= @maps %> 时,HTML 呈现 ActiveRecord::Relation:0x1044e8ba8&gt

在控制台中:

>> b= Map.last
=> #<Map id: 6, first_name: "James", last_name: "", address: "2478 Goldenrod Ln, Perrysburg, OH", address2: "", zip: "", city: "", phone: "", campaign_id: nil, email: "", employer: "", occupation: "", created_at: "2010-04-02 20:58:15", updated_at: "2010-04-02 20:58:15", latitude: 41.556996, longitude: -83.627157>
>> b.to_json
=> "{\"occupation\":\"\",\"city\":\"\",\"address\":\"2478 Goldenrod Ln, Perrysburg, OH\",\"zip\":\"\",\"latitude\":41.556996,\"created_at\":\"2010-04-02T20:58:15Z\",\"address2\":\"\",\"updated_at\":\"2010-04-02T20:58:15Z\",\"campaign_id\":null,\"id\":6,\"phone\":\"\",\"last_name\":\"\",\"employer\":\"\",\"longitude\":-83.627157,\"first_name\":\"James\",\"email\":\"\"}"

Rails 服务器:

我正在开发 Rails 3.0.0.beta2,遵循 Advanced Rails Recipes“食谱 #32,在 Google 地图上标记位置”,但遇到了障碍。

以下代码在第 3 行返回“ActiveSupport::JSON::Encoding::CircularReferenceError”“对象引用自身”。此代码表示 /layouts/maps.html.erb 文件

<% if @maps -%>
<script type="text/javascript">
    var maps = <%= @maps.to_json %>;
</script>
<% end -%>

这是我第一次尝试渲染 JSON,我不知道如何调试这个问题。你有这方面的经验吗?什么可能导致这个问题?

提前谢谢你!

【问题讨论】:

  • 我们需要知道@maps 是什么来帮助您调试。
  • 嗨,Ryan,我提供了更多信息。

标签: ruby-on-rails json


【解决方案1】:

我也看到了这个错误,并且发现它很难调试,因为在对象上调用 as_json 不会返回错误,而 to_json 会!?

尝试从模型中取出日期和时间属性,看看是否有帮助。我一直在谷歌上搜索这个问题,一些早期的解决方案(从 2007 年开始)建议修补 JSON 日期编码。

【讨论】:

  • 这可能是 3.0.0.beta2 的错误?我也发现这个事实令人费解。我对 as_json 也没有任何问题。
  • 我发现这是由需要 json 的捆绑 gem(联系人 gem)引起的。摆脱它并解决了许多与 json 相关的奇怪错误。
【解决方案2】:

我想通了!使用 google maps api 时,我的模型不能命名为“maps”。 to_json 在我创建新模型名称后起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多