I'm working on a single-page Rails app with heavy Google Maps integration and having trouble with the flow of data between Rails and Javascript.
My database has a cities table. A user clicks on a Google Maps marker of a city. I'd then like to pop an overlay which makes an active records query based on the city that was clicked and displays information about the city.
Currently, I'm firing an AJAX request when a user clicks on city and creates a Javascript event:
google.maps.event.addListener(marker,'click', function(event) {
var latitude = event.latLng.lat();
var longitude = event.latLng.lng();
$.ajax({
method: 'PUT',
url: '/cities',
data: { latitude: latitude, longitude: longitude },
dataType: 'json'
});
popup.style.display = 'block';
});
});
Which hits my controller's update function:
def update
latitude = params[:latitude]
longitude = params[:longitude]
@clicked_city = City.find_by(latitude: latitude, longitude: longitude)
end
This updates the @clicked_city
variable, but my view has no idea and can't make the active records query and show the info I'd like. Pretty sure I'm fundamentally not understanding how this is supposed to work in a RESTful app and hoping there's a much simpler way to do this.