我如何确定Rails资产管道中给定资产的MD5摘要? [英] How can I determine the MD5 digest of a given asset in the Rails asset pipeline?

查看:90
本文介绍了我如何确定Rails资产管道中给定资产的MD5摘要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ruby on Rails 3.1项目中编写了一个Javascript丰富的应用程序,并为我的JS模板框架使用了Handlebars。我试图找出一种方法来动态地将资产的MD5摘要(在资产预编译过程中生成)附加到我的Handlebars模板中的标记中。我希望有资产路径作为关键字和MD5摘要作为值的散列,但我一直无法找到它。



一个理想的解决方案是将Ruby中的散列值传递给Javascript,并定义一个Handlebars助手,该助手会自动将MD5摘要附加​​到资产的src属性中。



试图做类似的事情?必须有一种方法可以在Rails中使用Javascript模板,并且还可以享受资产指纹识别的好处。

解决方案

正如有人在评论中提到的那样,将资产路径附加散列值是资产管道的默认部分。 p>


在生产中,Rails将MD5指纹插入每个文件名,以便该文件通过Web浏览器缓存


您可以在资产管道中详细了解指纹识别 这里 。 Rails使用链接来编译资产。指纹是Sprockets进程的一部分。

您可以使用Sprockets的 find_asset 方法,传入逻辑路径到您的资产以获取 Sprockets :: BundledAsset 实例。例如

  [1] pry(main)> Rails.application.assets.find_asset('application.js')
=> #< Sprockets :: BundledAsset:0x3fe368ab8070 pathname =/ Users / deefour / Sites / MyApp / app / assets / javascripts / application.js,mtime = 2013-02-03 15:33:57 -0500,digest = ab07585c8c7b5329878b1c51ed68831e>

您可以在此对象上调用 digest_path 获得它的 MD5 总和追加到资产。

  [1] pry (主)GT; Rails.application.assets.find_asset('application.js')。digest_path 
=> application-ab07585c8c7b5329878b1c51ed68831e.js

有了这些知识,您可以轻松创建一个帮助程序来返回 digest_path ,然后从 .js.erb 文件中调用此帮助程序。


I'm writing a Javascript-rich application in a Ruby on Rails 3.1 project and using Handlebars for my JS templating framework. I'm trying to figure out a way to dynamically append the MD5 digest of an asset (generated during asset precompilation on production) to my tags inside of my Handlebars template. I'm hoping that there's a hash with the asset path as the key and the MD5 digest as the value, but I haven't been able to find one.

An ideal solution would be passing the hash from Ruby into Javascript and defining a Handlebars helper that would automatically append the MD5 digest to the "src" attribute of the asset.

Has anybody attempted to do something similar? There must be a way to use Javascript templates in Rails and also reap the benefits of asset fingerprinting.

解决方案

As someone mentioned in the comments, appending a hash to the asset paths is a default part of the asset pipeline.

In production, Rails inserts an MD5 fingerprint into each filename so that the file is cached by the web browser

You can read more about fingerprinting in the asset pipeline here. Rails uses Sprockets to compile assets. The fingerprinting comes as part of Sprockets process.

You can use Sprockets' find_asset method, passing in a logical path to your asset to get a Sprockets::BundledAsset instance. For example

[1] pry(main)> Rails.application.assets.find_asset('application.js')
=> #<Sprockets::BundledAsset:0x3fe368ab8070 pathname="/Users/deefour/Sites/MyApp/app/assets/javascripts/application.js", mtime=2013-02-03 15:33:57 -0500, digest="ab07585c8c7b5329878b1c51ed68831e">

You can call digest_path on this object to get it's MD5 sum appended to the asset.

[1] pry(main)> Rails.application.assets.find_asset('application.js').digest_path
=> "application-ab07585c8c7b5329878b1c51ed68831e.js"

With this knowledge you can easily create a helper to return the digest_path for any asset in your application, and call this helper from within your .js.erb files.

这篇关于我如何确定Rails资产管道中给定资产的MD5摘要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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