title: Rails passing variable to jBuilder
body: <p>Fixed and solutions incorporated into the OP. A combination of comments by @max here and to my related posting <a href="https://stackoverflow.com/questions/60610788/rails-connecting-to-jbuilder">Rails connecting to jBuilder</a>. Edits are with <code>// Was:</code></p>
  
  <p>I'm trying to get values through a <code>has_many :through</code> relationship. Three main databases: people, locations (which has street address and other information), and the join table, <code>years</code> which links the person to a particular address on a specific date. </p>
  
  <pre><code># models/person.rb
  class Person &lt; ApplicationRecord
    has_many :years, dependent: :destroy 
    has_many :locations, through: :years
  
  # models/location.rb
  class Location &lt; ApplicationRecord
    has_many :years
    has_many :people, through: :years
  
  # models/year.rb
  class Year &lt; ApplicationRecord
    belongs_to :location
    belongs_to :person
  </code></pre>
  
  <p><code>years</code> links the person to a particular address on a specific date.</p>
  
  <p>As I understand it the flow is that the <code>show.html.erb</code> calls <code>javascript/packs/olPersonMap.js</code> which then calls <code>show_locations.json.jbuilder</code>. How do I pass the <code>person.id</code> to jbuilder?</p>
  
  <p>From <code>views/people/show.html.erb</code> (class is probably not needed now, refactoring needed).</p>
  
  <pre><code>// Was: &lt;div id="map" class="map"&gt;&lt;/div&gt;
   &lt;div id="map" class="map" data-url="&lt;%= person_locations_path(@person) %&gt;"&gt;&lt;/div&gt;  
  &lt;%= javascript_pack_tag 'olPersonMap' %&gt;
  &lt;div&gt;
  </code></pre>
  
  <p>From <code>javascript/packs/olPersonMap.js</code>.</p>
  
  <pre><code>// Near the top of the file
  var urlJSON = $('#map').data('url') + '.json'
  // and much further down in the code
  new VectorImageLayer({
    title: 'Where lived and worked',
    imageRatio: 2,
    source: new VectorSource({
      // Was: url: '../people/show_locations',
      url: urlJSON
      format: new GeoJSON()
  }),
  </code></pre>
  
  <p>From @max 
  jBuilder was in <code>app/views/people/show_locations.json.jbuilder</code>, now <code>app/views/people/locations/index.json.jbuilder</code>. Leaving code out as it wasn't the main problem (although it did have few errors).</p>
  
  <p>And this </p>
  
  <pre><code># app/controllers/people/locations_controller.rb
  module People
    class LocationsController &lt; ApplicationController
      before_action :set_person
  
      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
  </code></pre>
  
  <p>and <code>routes.rb</code></p>
  
  <pre><code>resources :people do
    resources :locations, only: [:index], module: :people
  end
  </code></pre>
  
  <p>In summary, how do I pass person.id to the jBuilder? Or am I going at this all wrong?</p>
  
  <p>Thank you @max. I need to work on understanding what the controller is doing. Lots of moving parts. <code>https://secure-shore-68966.herokuapp.com/people/124</code> is the Leaflet version. Will push this OpenLayers version after some refactoring to remove all the trial stuff.</p>