JavaScript无法找到图像文件(Rails 4应用程序) [英] javascript can't find the image file (Rails 4 app)

查看:90
本文介绍了JavaScript无法找到图像文件(Rails 4应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在轨道4应用程序上有一个红宝石。在 app / assets / javascripts 中,我创建了一个文件 map.js 来在Google地图上绘制一些自定义标记:

  var marker = new google.maps.Marker({
draggable:false,
title:'Hi ',
图标:'icon1.png'
});

一切正常,除非找不到 icon.png 并给出错误消息 ActiveRecord :: RecordNotFound(找不到id = icon1的用户)。问题很可能在于如何编写文件地址,因为如果我将代码更改为图标:'https://maps.google.com/mapfiles/kml/shapes/schools_maps.png'一切正常,并在地图上显示标记。
如何找出如何解决问题?我查看了 log / development ,但没有找到任何有用的东西。
我应该在哪里放置 icon1.png 以及如何引用其地址?

请注意我不能在 map.js 中使用rails helper等。



非常感谢。

解决方案

如果您想在您的js文件中使用图片,那么首先您必须将您的js文件更改为 maps.js.erb ,然后您就可以通过 rails asset_path 这将允许你访问你的图片,所以你可以在你的js文件中有这个

  var marker = new google.maps.Marker({
draggable:false,
title:'Hi',
icon:< ;%= asset_path('icon1.png')%>//这将在资产/图片
}}中查找图片icon1.png;

编辑:

Rails资产管道基本上连接资产,这可以减少浏览器发出的请求数量。如果你看看导轨指南,它说



Sprockets将所有JavaScript文件连接成一个主.js文件和所有CSS文件到一个主.css文件中。 Rails会在每个文件名中插入一个MD5指纹,这样该文件就会被网页浏览器缓存



所以你不能指定你的由于这个原因,我们使用 rails asset_path 来为您的资产创建正确的网址


I have a ruby on rails 4 app. In app/assets/javascripts, I created a file map.js to draw some custom markers on google map:

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). 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. How can I find out how to solve the problem? I looked in log/development but didn't find anything useful there. Where should I put icon1.png and how should refer to its address?

Please note I can't use rails helpers, etc in map.js.

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

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. 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

这篇关于JavaScript无法找到图像文件(Rails 4应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆