I am new to Ruby on Rails and am playing around with it before I start my bootcamp soon. I am making a app which takes a latitude and longitude position and adds it to a leaflet map embedded on the page. I have created a database which has two values being latitude and longitude, which updates when the user adds their respective values.
The issue which I am having is I am unable to figure out how to retrieve these two values from the database to add to the javascript in the script tag as the latitude and longitude values (L.marker([**tasks.latitude**, **tasks.longitude**]).addTo(mymap);). I am aware I can retrieve the values inside an erb tag like <% @tasks.each do |task| %> then <%= task.latitude %>, but you cannot use a erb tag inside the script tag (or .js file which I will convert it to once I figure it out). Would I have to use AJAX or something else to grab the values? I am just a bit lost and hoping to be pointed in the right direction.
Below is the code from my index.html.erb and from the database in rails console
`<head>
<%= javascript_pack_tag 'application' %>
<%= stylesheet_pack_tag 'index' %>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<p id="notice"><%= notice %></p>
<header class='header'>
Parks Tasks
</header>
<main class="main">
<%= link_to 'New Task', new_task_path %>
<table class="table">
<thead>
<tr>
<th>Latitude</th>
<th>Longitude</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @tasks.each do |task| %>.
<tr>
<td><%= task.latitude %></td>
<td><%= task.longitude %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</main>
<aside class='mapid' id="mapid">
<script>
var mymap = L.map('mapid').setView([-38.35909, 144.937757], 13);
L.tileLayer(
'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1,
accessToken: 'removed for post',
}
).addTo(mymap);
L.marker([***tasks.latitude***, ***tasks.longitude***]).addTo(mymap);
</script>
</aside>
<footer class="footer">Footer</footer>
`
=> #<ActiveRecord::Relation [#<Task id: 1, latitude: -0.3836327e2, longitude: 0.144877609e3, created_at: "2020-07-18 09:21:05", updated_at: "2020-07-18 09:21:05">, #<Task id: 2, latitude: -0.3836327e2, longitude: 0.144877609e3, created_at: "2020-07-18 09:26:41", updated_at: "2020-07-18 09:26:41">]> irb(main):006:0>
I would appreciate any help on the matter and if you need any more clarification, please ask.
Regards, Rory.