ivanacorovic
ivanacorovic

Reputation: 2867

How to pass javascript variable into embeded ruby?

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

Upvotes: 0

Views: 45

Answers (1)

TGH
TGH

Reputation: 39248

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

Upvotes: 1

Related Questions