我正在创建一个迷你导航应用程序,我有 2 个模型(位置、路径),其中包含以下详细信息
地点
- 编号:整数
- 名称:字符串
- 纬度:十进制 {:precision => 8, :scale => 4}
- 经度:十进制 {:precision => 8, :scale => 4}
小路
- 开始:整数
- 结束:整数
在创建路径的表单上,我有 2 个从位置列表中填充的下拉菜单
<div class="field">
<%= form.label :start %>
<%= collection_select(:path, :start, Location.all, :id, :Name, prompt: true) %>
</div>
<div class="field">
<%= form.label :end %>
<%= collection_select(:path, :end, Location.all, :id, :Name, prompt: true) %>
</div>
在此下方,我想使用 MapBox 添加地图并显示起点和终点位置的标记,之前我使用以下代码将数据从控制器传递到视图并允许生成地图的 JQuery 访问它
<%= content_tag :div, class: "temp_information", data: {lat: @location.Latitude,long: @location.Longitude} do %>
<% end %>
<script>
map code
$('.temp_information').data('lat')
$('.temp_information').data('long')
</script>
如何从每个选定的位置提取纬度和经度并使其可用于 javascript?
编辑 - 添加了路径表单的代码
<%= form_with(model: path, local: true) do |form| %>
<% if path.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(path.errors.count, "error") %> prohibited this path from being saved:</h2>
<ul>
<% path.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= form.label :start %>
<%= form.select :start_id, options_for_select(Location.all.map{ |l| [l.Name, form.object.start_id, { data: { latitude: l.Latitude, longitude: l.Longitude } }] }), { prompt: true }, { class: "form-control" } %>
</div>
<div class="form-group">
<%= form.label :end %>
<%= form.select :finish_id, options_for_select(Location.all.map{ |l| [l.Name, form.object.finish_id, { data: { latitude: l.Latitude, longitude: l.Longitude } }] }), { prompt: true }, { class: "form-control" } %>
</div>
<div class="form-group">
<%= form.label :minutes %>
<%= form.number_field :minutes, class: "form-control" %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>