使用 jbuilder 构建多层

Building multiple layers with jbuilder

我有一层点和一层线。该层由 jbuilder 创建,Leaflet 显示它。 我可以单独创建和显示图层,但不能一起显示。 对于一层,map.js 有一部分

var featureLayer = L.mapbox.featureLayer()
                  .loadURL('map/map_data.geojson')
                  .addTo(map);
featureLayer.on('ready', function(e) {
  map.fitBounds(featureLayer.getBounds());
});

map_controller.rb:

def map_data
  start   = '1895-01-01'
  start   = start.to_date
  finish  = (start + 3.year)
  @lines =  RestoResidLine.where(resto_date: start..finish).select("id, person_id, resto_loc_id, resid_loc_id, resto_name, resto_date, title_resto, resid_date, title_resid, long_resto, lat_resto, long_resid, lat_resid")
end

map_data.json.jbuilder:

json.type "FeatureCollection"
json.features @lines do |line|
  if (line.long_resto && line.long_resid)
    json.type "Feature"
    json.geometry do
        json.type "LineString"
       json.coordinates [[line.long_resto, line.lat_resto], [line.long_resid, line.lat_resid]]
    end
  end 
end 

map_data.json 可供 map.js 使用。但我不明白所有这些是如何组合在一起的 Rails 魔力。

我想要带点的第二层。我首先尝试在控制器中添加第二个 json.features @points do |line|...end 调用 @lines。但只有最后一个出现。我认为他们可能会连接起来。我可能可以更改 @... 的定义以引入所有需要的信息,因此这将是一个解决方案;但逻辑会很丑陋。

所以我尝试创建另一个文件 point_data.json.jbuilder 并添加到 map.js

var featureLayer = L.mapbox.featureLayer()
                  .loadURL('map/point_data.geojson')
                  .addTo(map);

果然 point_data.json 得到构建,但它是 2400 行 html <title>Action Controller: Exception caught</title>map_data.json 已构建并正确显示。

这是命名问题吗?要么?我对 Rails、ActiveRecord 和 JavaScript Leaflet 比较陌生。

代码:https://bitbucket.org/MtnBiker/crores5/ 我会尝试上传该网站,以便您可以在(实际)操作中看到它。无法在 Heroku 上正确加载。

这不会那么难。谢谢。

我没有设置路由来查找 point_data,显然 jbuilder 需要它。

get 'map' => 'map#index'
get 'map/map_data',   :defaults => { :format => 'json' }
get 'map/point_data', :defaults => { :format => 'json' }
get 'map/line_data',  :defaults => { :format => 'json' }

前两个已经有了,后两个忘了加了。我现在用的是后两者。第一个是原来的,我把它分成了这两个。

当地图在 Heroku 上的工作方式与在本地主机上的工作方式不同时,部分帮助是挖掘

忘了这篇文章了。