Rails连接到jBuilder [英] Rails connecting to jBuilder

查看:61
本文介绍了Rails连接到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:22
(anonymous) @ mapPersonLeaflet.js:10

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

I'm trying to get values through a has_many :through relationship. Three main databases: people, locations (which has street address and other information), and the join table, years which links the person to a particular address on a specific date.

# 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将该人链接到特定日期的特定地址.

years links the person to a particular address on a specific date.

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

From views/people/show.html.erb (of course the page for a particular person which I hope to show details about the person including locations they were associated with).

<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中,则会出现错误:

I put the jBuilder file in a folder views/people/locations/ because if I put it in /people I got an error:

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) {失败,出现终端错误

Never gets to the above file. Fails at $.getJSON("locations/location_data.geojson", function (data_data) { with the error in Terminal

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

NameError - uninitialized constant People:

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

I can't figure out which People it's referring to. I know it's not getting to the .jbuilder doc because if I delete everything in that doc I get the same error.

我的路径有问题.错字了吗?

Something wrong with my paths. Typo somewhere?

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

PS.几个月前,我以OpenLayers脚本进行了尝试,但是由于我在Leaflet上取得了成功,因此回到了Leaflet,并希望回到我遇到相关问题的OpenLayers时

PS. I tried this a couple of months ago as an OpenLayers script, but came back to Leaflet since I have had success with Leaflet and hope to go back to the OpenLayers where I was having related problems Rails passing variable to jBuilder

推荐答案

我首先创建一个嵌套的路由,该路由提供JSON来填充地图:

I would start by just creating a nested route that serves up the JSON to populate the map:

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

您可以重命名模板/people/locations/index.json.jbuilder.

然后设置map元素,使其具有data属性,该属性告诉javascript从何处加载JSON:

Then setup the map element so that it has a data attribute that tells the javascript where to load the JSON from:

<%= content_tag :div, "",
        class: "map personal-map",
        data: { src: person_locations_path(@person, format: :json) }
%>

也要摆脱<%= javascript_pack_tag 'mapPersonLeaflet' %>-对内嵌脚本标签说不"

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

You can then just create a script in your assets pipeline that looks for elements with the personal-map class and augments them:

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元素上的数据属性.

If you want to pass any other information from the view to the map (like the bounds or zoom) use data attributes on the map element.

这篇关于Rails连接到jBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆