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);
    };

1 Answer 1

1

In order to pass data from JavaScript to the server you have to make an ajax call

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

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.