[英]javascript can't find the image file (Rails 4 app)
I have a ruby on rails 4 app.我有一个 ruby on rails 4 应用程序。 In
app/assets/javascripts
, I created a file map.js
to draw some custom markers on google map:在
app/assets/javascripts
,我创建了一个文件map.js
来在谷歌地图上绘制一些自定义标记:
var marker = new google.maps.Marker({
draggable: false,
title: 'Hi',
icon: 'icon1.png'
});
Everything is working fine, except it can't find icon.png
and gives me the error message ActiveRecord::RecordNotFound (Couldn't find User with id=icon1)
.一切正常,除了它找不到
icon.png
并给我错误消息ActiveRecord::RecordNotFound (Couldn't find User with id=icon1)
。 The problem is most likely with how to write the file address, because if I change the code to icon: 'https://maps.google.com/mapfiles/kml/shapes/schools_maps.png'
everything works perfectly and it displays the marker on the map.问题很可能与如何编写文件地址有关,因为如果我将代码更改为
icon: 'https://maps.google.com/mapfiles/kml/shapes/schools_maps.png'
一切正常,它会显示地图上的标记。 How can I find out how to solve the problem?我怎样才能知道如何解决这个问题? I looked in
log/development
but didn't find anything useful there.我查看了
log/development
但没有发现任何有用的东西。 Where should I put icon1.png
and how should refer to its address?我应该把
icon1.png
放在icon1.png
以及应该如何引用它的地址?
Please note I can't use rails helpers, etc in map.js
.请注意,我不能在
map.js
使用 rails helpers 等。
Thanks a lot.非常感谢。
If you want to use images in your js file then first of all you'll have to change your js file to maps.js.erb
and then you can access your assets by rails asset_path
which will allow you to access your images , so you can have this in your js file如果你想在你的 js 文件中使用图像,那么首先你必须将你的 js 文件更改为
maps.js.erb
然后你可以通过rails asset_path
访问你的资产,这将允许你访问你的图像,所以你可以在你的 js 文件中有这个
var marker = new google.maps.Marker({
draggable: false,
title: 'Hi',
icon: "<%= asset_path('icon1.png') %>" //this will look for an image "icon1.png" in assets/images
});
Edit:编辑:
Rails asset pipeline basically concatenate assets, which can reduce the number of requests that a browser makes. Rails 资产管道基本上是连接资产,这可以减少浏览器发出的请求数量。 If you look at rails guides it says
如果您查看导轨指南,它会说
Sprockets concatenates all JavaScript files into one master .js file and all CSS files into one master .css file. Rails inserts an MD5 fingerprint into each filename so that the file is cached by the web browser
So you can't specify your assets path by a static url for this reason we use rails asset_path
which will make proper url for your assets因此,您不能通过静态 url指定您的资产路径,因此我们使用
rails asset_path
它将为您的资产生成正确的 url
You can try to use gem js_assets
你可以尝试使用 gem
js_assets
In your application.js在你的 application.js 中
//= require app_assets
This directive adds the method asset_path in the global scope.该指令在全局范围内添加了方法 asset_path。
Add to filter files *.png
.添加到过滤器文件
*.png
。 To do this, add initizlizer:为此,请添加初始化程序:
# config/initializers/js_assets.rb
JsAssets::List.allow << '*.png'
To get the path to the file icon1.png
depending on the environment根据环境获取文件
icon1.png
的路径
var marker = new google.maps.Marker({
draggable: false,
title: 'Hi',
icon: asset_path('icon1.png')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.