【发布时间】:2017-04-20 02:37:45
【问题描述】:
刚接触 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));
}
位置控制器:
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"
【问题讨论】:
-
刚刚注意到,我删除了第二个 var places = '';从循环内部。不过同样的问题。
-
您好,请补充完整的错误信息
-
"#<Location::ActiveRecord_Relation:0x007f8d7939e758>"是我得到的唯一错误,它只在我输入“地点”时显示在控制台中。没有其他错误。
-
Location::ActiveRecordRelation不是错误,它是代表ActiveRecord::Relation的类,您在控制台中看到的内容很可能是@locations.inspect的结果 -
Uncaught SyntaxError: Invalid or unexpected token 我认为@locations = Location.all 会负责使变量对视图可用。
标签: javascript ruby-on-rails google-maps