Reputation: 77
I have a Rails 4 application. I want to display locations on google maps street view. Longitude and latitude are saved in database, so it can only accessed through rails. Controller :
@posts.each do |post|
markers << post.marker_single_show
end
Javascript :
function addMarker(latLng, map, icon) {
if (formMarker !== undefined) {
formMarker.setMap(null);
}
formMarker = new google.maps.Marker({
position: latLng,
animation: google.maps.Animation.BOUNCE,
title: "localisation",
icon: icon
});
formMarker.setMap(map);
var latlngbounds = new google.maps.LatLngBounds();
latlngbounds.extend(latLng);
map.fitBounds(latlngbounds);
map.panToBounds(latlngbounds);
}
As you can notice, when using addMarker function I should provide latlng value which is rendred by rails controller. How can I pass values from rails to Javascript?
Upvotes: 1
Views: 1743
Reputation: 2312
app/controllers/some_controller.rb
def someMethod
@railsVariable = Model.all or Model.find(id)
end
in its view you can do something like this
<script>
var JavascriptVariable = <%= @railsVariable %>;
</script>
Hope this will help you :)
Upvotes: 4