Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby generated javascript file is not updated on page refresh

I am working on an application where items stored in database are displayed on Google map. For that purpose I added gmap.js.erb file to app/assets/javascripts folder. Items are fetched from the corresponding table and put on the map in the following way:

function showItemsOnMap() {
  <% Item.all.each do |item| %>
    var lat = <%= item.lat %>;
    var lng = <%= item.lng %>;
    addMarker(lat, lng);
  <% end %>
}

This works fine and items are displayed correctly, but when I add a new item and refresh my map page, the newly added item is not displayed. gmap.js.erb is not updated after I make changes to the database and click refresh. I guess it is not enough to trigger gmap.js.erb update just by clicking refresh button in the browser. How should I do that? How one makes gmap.js.erb updated whenever Items table is modified?

like image 790
Sergii Gryshkevych Avatar asked Jun 20 '26 14:06

Sergii Gryshkevych


1 Answers

Your gmap.js.erb is compiled to gmap.js and cached at the time of first page request which uses this js file. You can put these js inside view to make it a dynamic script.

view.html.erb

<script>
function showItemsOnMap() {
  <% Item.all.each do |item| %>
    var lat = <%= item.lat %>;
    var lng = <%= item.lng %>;
    addMarker(lat, lng);
  <% end %>
}
</script>
like image 85
dileep nandanam Avatar answered Jun 23 '26 03:06

dileep nandanam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!