在rails应用程序中更正从js到assets/images的路径

Correct path from js to assets/images path in rails app

本文关键字:assets images 路径 js 应用程序 rails      更新时间:2023-09-26

如何在js.erb文件中正确写入"assets/images/"的路径?我正在做:

icon: createImage('assets/'+img),

有趣的是,这在localhost中有效,但在heroku中无效。在stackoveflow中搜索后,我尝试了这个:

icon: createImage('<%= asset_path(img) %>'),

我想这个应该对我有帮助。但是我不能把img传递给asset_path

注意:我在assets/javascript/map/gmap.js.erb 中进行

编辑:我在rails指南中发现,应用程序/资产中的文件从未在生产中直接提供。所以现在我知道为什么它在生产中不起作用了。http://guides.rubyonrails.org/asset_pipeline.html

您可以尝试:

icon: createImage('<%= image_tag(img) %>')

特别是如果您正在运行Rails 3.1+并使用Asset Pipeline,那么您希望使用Rails辅助对象来指定文件,而不是硬编码的文件路径。这应该在/assets中查找img文件,如下所述。