简体   繁体   中英

Rails 4 - how do I get the images path of an image in production with a hash on the end?

I want to reference an image in the public folder that has been precompiled in prod. But, it seems that all the images have a hash on the end. (ie, assets/image-3414fewafe313.jpg)

asset_path(photo) = assets/photo.jpg (i need the full image path with hash)

How do I reference this image in a view in Rails? Thanks!

For a view, you can just reference image_path('photo.jpg')

http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

See also image_tag('photo.jpg') which produces a full HTML img tag.

The hash at the end of the asset's URL is the result of having the config.assets.digest parameter set to true

Quoting from http://edgeguides.rubyonrails.org/asset_pipeline.html#in-production :

In the production environment Sprockets uses the fingerprinting scheme outlined above. By default Rails assumes assets have been precompiled and will be served as static assets by your web server.

During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disk. These fingerprinted names are used by the Rails helpers in place of the manifest name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM