問題描述
使用 RoR 4.1.4
Using RoR 4.1.4
我正在嘗試使用 leaflet-rails gem.我按照 github 頁面中概述的步驟進行操作,但是當我嘗試加載地圖時,我看到了
I am trying to use the leaflet-rails gem. I followed the steps outlined in the github page, but when I try to load the map, I see
ReferenceError: L is not defined
在瀏覽器控制臺中.這顯然意味著 gem 中的幫助程序正在加載和執行,但它找不到 leaflet.js
文件.
in the browser console. This obviously means that the helper from the gem is being loaded and executed but it can't find the leaflet.js
file.
但是,頁面的 head 部分顯示 /assets/leaflet.js
正在被引用并且它實際上就在那里.
However, the head section of the page shows that /assets/leaflet.js
is being referenced and it actually IS there.
當我查看生成的代碼時:
When I look at the generated code:
<div id="map"></div>
<script>
var map = L.map('map')
map.setView([-54.0, 6.08], 16)
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a >OpenStreetMap</a> contributors',
maxZoom: 18,
subdomains: '',
}).addTo(map)
</script>
</div>
<script src="/assets/jquery.js?body=1" data-turbolinks-track="true"></script>
<!-- all the other scripts loaded -->
<script src="/assets/exif.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/leaflet.js?body=1" data-turbolinks-track="true"></script>
<!-- some more scripts -->
因此,gem 在地圖 div 的正下方添加了一個腳本,然后,由于 sprockets 機制和所有這些,其他腳本才被加載.對我來說,這看起來很明顯腳本不可能加載leaflet.js,因為它被之后引用!
So the gem adds a script right below the map div, and only then, due to the sprockets mechanism and all that, the other scripts are loaded. To me this looks like then obviously the script can't possibly load the leaflet.js as it's being referenced afterwards!
那么...我是否誤解了 RoR 如何處理 gems 和 javascripts?它一定在某個時候起作用了……
So...am I misunderstanding something re how RoR handles gems and javascripts? It must have been working at some point...
這是我的/app/assets/javascript/application.js:
Here's my /app/assets/javascript/application.js:
//= require jquery
//= require jquery.ui.widget
// Here I load a whole bunch of javascripts which are related to jquery-fileupload, cut for brevity
//= require bootstrap.min
//= require bootbox.min
//= require bootstrap-datepicker
//= require exif
//= require leaflet
//= require turbolinks
//= require_tree .
推薦答案
不知道你是否能解決你的問題.對我來說,我忘記了跑步:
Don't know if you were able to solve your issue. For me, I had forgotten to run:
rake assets:precompile
在部署到生產之前.
這篇關于ruby on rails:傳單導軌未加載的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!