gpt4 book ai didi

javascript - 查看错误消息: "#<Location::ActiveRecord_Relation:0x007f8d79d3de30>"

转载 作者:太空宇宙 更新时间:2023-11-04 15:46:40 26 4
gpt4 key购买 nike

Rails 新手,正在尝试使用 Google Maps API。我希望循环遍历并显示数据库中每个地理位置的标记。当我在窗口中打开控制台并查找变量 [places](该变量应具有我所有 @locations 的值)时,我收到上述错误消息。有什么帮助吗?谢谢。

map.html.erb:(

 <h1>map Page</h1>

 map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(37.725685, -122.156830),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var places = '<%= @locations %>';
var infowindow = new google.maps.InfoWindow();
var marker, i;

for (i = 0; i < places.length; i++) {
var places = '<%= @locations %>';
marker = new google.maps.Marker({
position: new google.maps.LatLng(places[i].lat, places[i].lng),
map: map
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(places[i].description);
infowindow.open(map, marker);
}
})(marker, i));
}

位置 Controller :

def map
@locations = Location.all
end

位置模型:

class CreateLocations < ActiveRecord::Migration[5.0]
def change
create_table :locations do |t|
t.string :location_name
t.string :location_address
t.string :location_description
t.float :lat
t.float :lng

t.timestamps
end
end
end

路线:

get '/locations/map', to: "locations#map", as: "map"

最佳答案

Rails 不知道如何序列化您的 @locations 变量,请尝试类似的操作(基于您想要的):

var places = <%= @locations.to_json.html_safe %>

关于javascript - 查看错误消息: "#&lt;Location::ActiveRecord_Relation:0x007f8d79d3de30&gt;",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43509250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com