Dan Klos
Dan Klos

Reputation: 709

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

I need to iterate through multiple images via a JS file, so I am making an http request to the server to match the filename to its client-side fingerprinted equivalent. I'm getting the fingerprinted file by passing the original file through Rails.application.assets.find_asset(file).digest_path

Example, in the rails console:

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

returns

"scene1-b691b411ad644bcf2c84ef9e30f52db9ffdf57c18fadf99872dff3ebb81fa548.jpg"

However, when I run on my local server the output is simply nil.

Upvotes: 1

Views: 1534

Answers (1)

myconode
myconode

Reputation: 2617

Using the Rails asset pipeline, assets are referenced with their logical path. In this case, you can use embedded ruby (erb) in your script by adding the .erb extension to the file, e.g.

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

When this script is compiled, the ruby code will produce a reference to the asset with the current digest. The digest path is a "fingerprint" and will change as often as the assets themselves change.

To learn more about the asset pipeline, check out the ROR guide and the Sprockets documentation.

Upvotes: 1

Related Questions