如何编写此 Rails 4.2 js 资产路径

How to write this Rails 4.2 js asset path

本文关键字:js 路径 何编写 Rails      更新时间:2023-09-26

我在供应商目录中的.js文件中有这个:

arrowpointers:{
    downarrow: ["img/arrow-down.png", 11,7], //[path_to_down_arrow, arrowwidth, arrowheight]
    rightarrow: ["img/arrow-right.png", 12,12], //[path_to_right_arrow, arrowwidth, arrowheight]
    showarrow: {toplevel: true, sublevel: true} //Show arrow images on top level items and sub level items, respectively?
   },

我无法展示arrow-down.pngarrow-right.png。我已经尝试了资产和图像路径、url 等的所有组合。所有图像都在app/assets/images文件夹中。其他所有图像都工作正常,但我无法弄清楚。有什么想法吗?

将文件扩展名更改为 js.erb ,以便我们可以在其中嵌入 ruby 代码。将图像添加到文件夹中app/assets/images。然后在application.rb中添加此行以启用资产,

class Application < Rails::Application
 #add this line
 config.assets.precompile += %w( *.js *.css *.png *.jpg *.jpeg )
end

最后在您的js.erb文件中进行此更改,

arrowpointers:{
    downarrow: ['<%= asset_path("arrow-down.png") %>', 11,7], //[path_to_down_arrow, arrowwidth, arrowheight]
    rightarrow: ['<%= asset_path("arrow-right.png") %>', 12,12], //[path_to_right_arrow, arrowwidth, arrowheight]
    showarrow: {toplevel: true, sublevel: true} //Show arrow images on top level items and sub level items, respectively?
   },