Rails.application.assets.find_assets(file).digest_path 在生产中不起作用

Rails.application.assets.find_assets(file).digest_path doesn't work in production

我需要通过一个 JS 文件遍历多个图像,因此我向服务器发出了一个 http 请求,以将文件名与其客户端指纹等效项相匹配。我通过 Rails.application.assets.find_asset(file).digest_path

传递原始文件来获取指纹文件

例如,在 rails 控制台中:

Rails.application.assets.find_asset("scene1.jpg").digest_path

returns

"scene1-b691b411ad644bcf2c84ef9e30f52db9ffdf57c18fadf99872dff3ebb81fa548.jpg"

但是,当我在本地服务器上 运行 时,输出只是 nil

使用 Rails 资产管道,通过向文件添加 .erb 扩展名,在您的脚本中通过 logical path. In this case, you can use embedded ruby (erb) 引用资产,例如

var logo = <%= asset_path('logo.png') %>;

编译此脚本时,ruby 代码将生成对具有当前摘要的资产的引用。摘要路径是“fingerprint”,并且会随着资产本身的变化而变化。

要了解有关资产管道的更多信息,请查看 ROR guide and the Sprockets documentation