ruby-on-railsleafletjbuilder

Rails connecting to jBuilder


Chrome error:

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

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 links the person to a particular address on a specific date.

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>

From 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

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

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:

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?

From routes.rb I have this line get 'people/locations/location_data', :defaults => { :format => 'json' }

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


Solution

  • 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
    

    Then setup a controller:

    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
    

    You can rename the template /people/locations/index.json.jbuilder.

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

    Also get rid of <%= javascript_pack_tag 'mapPersonLeaflet' %> - just say no to inline script tags!

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

    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.