I'm developing a Rails app with Leaflet. I have model Pipe with field location that is type linestring. I want to be able to click on the map, get the coordinates of that spot and return number of pipes that are at certain distance of it. I can't return coordinates to Ruby. I realize that Ruby gets executed before Javascript, so how do I do it? This is the function in my .js.erb file:
function onMapClick(e) {
var sql = <%= Pipe.where{st_dwithin((location),ST_GeomFromText("POINT(#{e.latlng.lat} #{e.latlon.lon})", 4326), 3000)}.count.to_s %>;
map.openPopup(sql.toString(), e.latlng);
};
Alexander
4,5527 gold badges31 silver badges46 bronze badges
asked Nov 21, 2014 at 4:42
ivanacorovic
2,8974 gold badges34 silver badges51 bronze badges
1 Answer 1
In order to pass data from JavaScript to the server you have to make an ajax call
answered Nov 21, 2014 at 4:44
TGH
39.4k13 gold badges106 silver badges140 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
ivanacorovic
Could you show me an example? Do I need to make it return .json somewhere?
TGH
You could maybe find some help here stackoverflow.com/questions/7237720/jquery-post-to-rails
ivanacorovic
function onMapClick(e) { $.ajax({ url: "/temps", type: "POST", data: {coordinates: {lat: e.latlon.lat, lon: e.latlon.lon}}, success: function(response) { } }); }; map.on('click', onMapClick); I hate not being able to make new line here :/ Sorry. I'm trying this, but can't get it work at all.lang-js