简体   繁体   English

如何将javascript变量传递到嵌入红宝石中?

[英]How to pass javascript variable into embeded ruby?

I'm developing a Rails app with Leaflet. 我正在用Leaflet开发一个Rails应用程序。 I have model Pipe with field location that is type linestring . 我的模型Pipe的字段位置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. 我无法将坐标返回给Ruby。 I realize that Ruby gets executed before Javascript, so how do I do it? 我意识到Ruby在Javascript之前执行,所以我该怎么做? This is the function in my .js.erb file: 这是我的.js.erb文件中的函数:

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

为了将数据从JavaScript传递到服务器,您必须进行ajax调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM