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 codeposts/show.html.erb
<% = render'map', {lat: @lat, lng: @lng}%>
Partial template (_map.html.erb)
<style>
#map {
height: 300px;
width: 300px;
}
</style>
<script>
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>
<script src = "https://maps.googleapis.com/maps/api/js? language = ja®ion = JP&key =<% = ENV ["Google_Maps_API_Key "]%>&callback = initMap" async defer></script>
posts_controller.rb
def show
@user = @ post.user
@spot = Spot.find_by (post_id: params [: id])
@lat = @ spot.latitude
@lng = @ spot.longitude
end
The Spot model has latitude and longitude, which is @lat, @lng.
Supplement
Ruby 2.6.6
Rails 5.2.4
If i know the cause and solution, please answer.
-
Answer # 1
Related articles
- ruby - partial template is not displayed (rails)
- ruby on rails - i want to change only a part of the partial template
- ruby on rails - about aws environment variables
- ruby on rails - i want to reflect the partial template only when i am logged in
- ruby - [rails] about partial templates
- ruby on rails 6 - i changed the project name of rails, but it became inaccessible
- ruby on rails - i want to resolve rails nameerror
- ruby on rails - i can't update rubygems
- ruby - about "activemodel :: unknownattributeerror" of rails
- ruby on rails 6 - rails6 activerecord :: recordinvalid in userscontroller # update
- ruby - [rails] i want to change the argument of link_to using conditional branching
- ruby - the last db setting doesn't work when deploying rails app on aws
- ruby on rails - i want to jump to the action of another controller with render
- ruby - rails causes activerecord :: recordnotfound and routing error at the same time
- ruby on rails - heroku: how to check the database_url used for regular backup
- ruby on rails 5 - i want to update the received parameters with nil
- ruby on rails - it is not saved in the database after registering the product
- ruby on rails - mvc rails
- ruby on rails - rails6 rspec model run-time error nomethoderror: undefined method `valid?'for nil: nilclass
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
Isn't it including Turbolinks?
If you're developing something like writing JavaScript directly in a view, the correct answer is to stop Turbolinks.