I am making an app that has a posting function linked to a map. You will be asked to enter your address when posting, and it will be automatically replaced with latitude/longitude and saved in the database.
I want to get latitude/longitude information from the database, receive it as a variable with the Google Maps API which is a partial template, and display the map on the post details page.

When you access the detail page of any post, the location information of the post you were looking at before is inherited, and you can not display the location of the post i am currently viewing unless you reload it once.

Corresponding source code


<% = render'map', {lat: @lat, lng: @lng}%>

Partial template (_map.html.erb)

  #map {
    height: 300px;
    width: 300px;
  let map
  function initMap () {
    geocoder = new google.maps.Geocoder ()
    map = new google.maps.Map (document.getElementById ('map'), {
      center: {lat:<% = lat%>, lng:<% = lng%>}, ← Passing variables here
      zoom: 15, 15,
    marker = new google.maps.Marker ({{
      position: {lat:<% = lat%>, lng:<% = lng%>},
      map: map
<script src = "https://maps.googleapis.com/maps/api/js? language = ja&region = JP&key =<% = ENV ["Google_Maps_API_Key "]%>&callback = initMap" async defer></script>


def show
    @user = @ post.user
    @spot = Spot.find_by (post_id: params [: id])
    @lat = @ spot.latitude
    @lng = @ spot.longitude
The Spot model has latitude and longitude, which is @lat, @lng.

Ruby 2.6.6
Rails 5.2.4

If i know the cause and solution, please answer.