Google Maps integration with JSON - CircularReferenceError
Update II
I Figured this out. When using google maps api, the model must not be named "maps". to_json works after I created a new model name.
Update
@maps is the name of my model, it contains information such as address, longitude, latitude. When I replace <%= @maps.to_json %>; to <%= @maps %>
, the HTML renders ActiveRecord::Relation:0x1044e8ba8>
In console:
>> b= Map.last
=> #<Map id: 6, first_name: "James", last_开发者_如何学JAVAname: "", 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 Server:
I'm working on Rails 3.0.0.beta2, following Advanced Rails Recipes "Recipe #32, Mark locations on a Google Map" and I hit a road block.
The following code is returning a "ActiveSupport::JSON::Encoding::CircularReferenceError" "object references itself" at line 3. This code represents the /layouts/maps.html.erb file
<% if @maps -%>
<script type="text/javascript">
var maps = <%= @maps.to_json %>;
</script>
<% end -%>
This is my first attempt at rendering JSON, and I don't know how to debug this problem. Do you have experience with this? What could cause this problem?
Thank you in advance!
I am seeing this error as well & finding it very difficult to debug since calling as_json on the object does not return errors, while to_json does!?
Try taking out the date & time attributes out of your model & see if that helps. I have been googling this problem & some earlier solutions (from 2007) recommend patching JSON date encoding.
I Figured this out! when using google maps api, my model must not be named "maps". to_json works after I created a new model name.
精彩评论