Ruby on rails 连接到jBuilder的Rails

Ruby on rails 连接到jBuilder的Rails,ruby-on-rails,leaflet,jbuilder,Ruby On Rails,Leaflet,Jbuilder,Chrome错误: jquery.js:8678 GET http://localhost:3000/people/locations/location_data.geojson 404 (Not Found) send @ jquery.js:8678 ajax @ jquery.js:8327 jQuery.<computed> @ jquery.js:8464 getJSON @ jquery.js:8448 makeMapTwo @ mapPersonLeaflet.js:2

Chrome错误:

jquery.js:8678 GET http://localhost:3000/people/locations/location_data.geojson 404 (Not Found)
send @ jquery.js:8678
ajax @ jquery.js:8327
jQuery.<computed> @ jquery.js:8464
getJSON @ jquery.js:8448
makeMapTwo @ mapPersonLeaflet.js:22
(anonymous) @ mapPersonLeaflet.js:10
years
将此人链接到特定日期的特定地址

views/people/show.html.erb
(当然是针对特定人员的页面,我希望显示该人员的详细信息,包括与他们相关的位置)

我将jBuilder文件放在一个文件夹
views/people/locations/
中,因为如果我将其放在
/people
中,我会得到一个错误:

ActiveRecord::RecordNotFound - Couldn't find Person with 'id'=location_data:
  app/controllers/people_controller.rb:118:in `set_person'
views/people/locations/location\u data.json.jbuilder

json.type "FeatureCollection"
json.features @person.years do |year|
# Person has_many :locations, through: :years  
  json.type "Feature"    
  json.properties do
    json.set! "marker-color", "#C978F3" # will make this different for resto and resid a
     # json.title year.resto
    json.name year.full_name
  end
  json.geometry do
     json.type "Point"
     json.coordinates [year.location['longitude'], year.location['latitude']]
  end # json.geometry
end # features
永远无法访问上述文件。在
$.getJSON(“locations/location\u data.geojson”)函数(data\u data){
处失败,终端出错

Started GET "/people/locations/location_data.geojson" for ::1 at 2020-03-09 18:47:05 -0700

NameError - uninitialized constant People:
我不知道它指的是哪个
。我知道它没有到达.jbuilder文档,因为如果我删除该文档中的所有内容,我会得到相同的错误

我的路径有点问题。哪里有打字错误

routes.rb
我有一行
get'people/locations/location_data',:defaults=>{:format=>'json'}


另外,几个月前,我曾尝试将其作为OpenLayers脚本使用,但由于我已成功使用了传单并希望返回到我遇到相关问题的OpenLayers,因此返回传单。您的路线未指定其映射到的控制器和操作,请添加
:to
参数以修复此问题:

get 'people/locations/location_data', :to => 'people#location_data', :defaults => { :format => 'json' }

我假设路由器以不同的方式解释路由。

我首先创建一个嵌套路由,提供JSON来填充映射:

resources :people do
  # ...
  resources :locations, only: [:index], module: :people
end
然后设置控制器:

module People
  class LocationsController < ApplicationController
    before_action :set_person
    # GET /people/1/locations.json
    def index
      respond_to do |f|
        f.json
      end
    end

    private
    def set_person
      @person = Person.eager_load(:locations)
                      .find(params[:person_id])
      @locations = @person.locations
    end
  end
end
也要摆脱
——只需拒绝内联脚本标记

然后,您可以在资产管道中创建一个脚本,用
personal map
类查找元素并对其进行扩充:

function personalMap(el){
  var map = L.map(el, { center: [34.040951, -118.258579], zoom: 13 });
  L.tileLayer('https://crores.s3.amazonaws.com/tiles/bkm/{z}/{x}/{y}.png')
    .addTo(map);
  $.getJSON(el.dataset.src).done(function(data){
    L.geoJSON(data).addTo(map)
  });
  return map;
}

document.addEventListener('DOMContentLoaded', function () {
  // you could also use $('.personal-map').each or the less sucky ES6
  // equivilent
  var personalMaps = Array.prototype.map.call(
    document.getElementsByClassName('personal-map'), 
    personalMap
  );
});

如果要将任何其他信息从视图传递到地图(如边界或缩放)在map元素上使用数据属性。

我故意选择了不使用geoJSON内容类型。它可以与JSON一起工作,并且不需要太多的修改。感谢您提高我的知识。content_标记没有正确解析。我进入了
{:class=>“map personal map”,:data=>{:src=>“/people/108/locations.JSON}
当然,geojson不会生成,地图也不会显示。
rails路线
=>`person\u locations get/people/:person\u id/locations(:格式)人物/地点#索引`并没有给我提供任何线索是的,我应该意识到它需要第二个与内容(或块)相关的参数这就是为什么它将选项视为内容。编辑。还有官方的rails文档。apidock只是一个让人讨厌的网站,有很多谷歌汁。
但是
位置。json
没有被构建。Chrome和终端都没有错误。我对模块提出了问题,但是
包含位置控制器
,然后结果是我n一个错误
错误的参数类型类(预期模块)
,这表明这不是引入模块的正确方法。错误是
包含
,因为清空关注文件会导致相同的错误。“未构建”实际指的是什么?发送ajax请求(检查chrome中的网络选项卡)?反应如何?
resources :people do
  # ...
  resources :locations, only: [:index], module: :people
end
module People
  class LocationsController < ApplicationController
    before_action :set_person
    # GET /people/1/locations.json
    def index
      respond_to do |f|
        f.json
      end
    end

    private
    def set_person
      @person = Person.eager_load(:locations)
                      .find(params[:person_id])
      @locations = @person.locations
    end
  end
end
<%= content_tag :div, "",
        class: "map personal-map",
        data: { src: person_locations_path(@person, format: :json) }
%>
function personalMap(el){
  var map = L.map(el, { center: [34.040951, -118.258579], zoom: 13 });
  L.tileLayer('https://crores.s3.amazonaws.com/tiles/bkm/{z}/{x}/{y}.png')
    .addTo(map);
  $.getJSON(el.dataset.src).done(function(data){
    L.geoJSON(data).addTo(map)
  });
  return map;
}

document.addEventListener('DOMContentLoaded', function () {
  // you could also use $('.personal-map').each or the less sucky ES6
  // equivilent
  var personalMaps = Array.prototype.map.call(
    document.getElementsByClassName('personal-map'), 
    personalMap
  );
});