2017-04-20 4 views
1

Google Maps APIを使用しています。私はループし、データベース内の各ジオロケーションにマーカーを表示することを検討しています。ウィンドウでコンソールを開いて、変数[places](すべて@locationsの値が必要)を探すと、上記のエラーメッセージが表示されます。助けてください?ありがとうございました。エラーメッセージ "#<場所:ActiveRecord_Relation:0x007f8d79d3de30 >"

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" 
+0

ちょうど気付いて、2番目の0を削除しましたvar places = '<%= @locations%>';ループの内側から しかし、同じ問題。 – ZeroCalm

+0

こんにちは、完全なエラー情報 – Bohdan

+0

を追加してください「#<場所:: ActiveRecord_Relation:0x007f8d7939e758 >」 は私が得る唯一のエラーであり、そしてそれだけで私は「場所」を入力したときのint彼はコンソール表示されます。その他のエラーはありません。 – ZeroCalm

答えて

1

Railsは(あなたが欲しいものを上ベース)のような何かをしようと、変数あなた@locationsをシリアル化する方法がわかりません。

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

それは、私は私のオブジェクトにアクセスできました。ありがとうございました! – ZeroCalm

関連する問題