Rails连接到jBuilder

问题描述 投票:0回答:1

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

我正在尝试通过has_many :through关系获取值。三个主要数据库:人员,位置(具有街道地址和其他信息)以及联接表years,联接表将人员在特定日期链接到特定地址。

# models/person.rb
class Person < ApplicationRecord
  has_many :years, dependent: :destroy 
  has_many :locations, through: :years

# models/location.rb
class Location < ApplicationRecord
  has_many :years
  has_many :people, through: :years

# models/year.rb
class Year < ApplicationRecord
  belongs_to :location
  belongs_to :person

[years将人员链接到特定日期的特定地址。

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

<div id="map" class="map"></div>  
  <%= javascript_pack_tag 'mapPersonLeaflet' %>
<div>

来自javascript/packs/mapPersonLeaflet.js

document.addEventListener('DOMContentLoaded', function () {
  makeMapTwo(); // LINE 10 in Chrome error above
});
function makeMapTwo(){
  var mapVar = L.map("map", { center: [34.040951, -118.258579], zoom: 13 });
  L.tileLayer('https://crores.s3.amazonaws.com/tiles/bkm/{z}/{x}/{y}.png').addTo(mapVar);
  $.getJSON("locations/location_data.geojson", function (data_data) { // LINE 22 in Chrome error above

我将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_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_data.geojson", function (data_data) {失败,出现终端错误>

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

NameError - uninitialized constant People:

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

我的路径有问题。错字?

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

PS。我在几个月前以OpenLayers脚本的方式进行了尝试,但是由于我在Leaflet上取得了成功,因此回到了Leaflet,希望回到我遇到相关问题的OpenLayers上Rails passing variable to jBuilder

Chrome错误:jquery.js:8678 GET http:// localhost:3000 / people / locations / location_data.geojson 404(未找到)发送@ jquery.js:8678 ajax @ jquery.js:8327 jQuery。@ jquery.js:8464 ...

ruby-on-rails leaflet jbuilder
1个回答
0
投票

您的路线未指定控制器及其映射到的操作,请添加:to参数来解决此问题:

© www.soinside.com 2019 - 2024. All rights reserved.