0

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

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Could you show me an example? Do I need to make it return .json somewhere?
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.