简体   繁体   中英

Rails Asset Pipeline Paths In Runtime Javascript

I have a JavaScript file that consumes a JSON file generated from a rails model and placed into the page by a controller.

Within this JSON file are a number of nodes, each with an id which I need to take and convert to image paths. In development I know where the images will be so I can loop through the do:

var imagePath = 'some/path/'+node.id+'.png';

However I am using Fingerprinting, so on staging and production I need a way to resolve this path to a fingerprinted path.

So far as I can see I need to expose a map of paths - fingerprinted paths so I can effectively do something like this:

var fingerprintedImagePath = fingerprintedPathsMap[imagePath];

In order to do this I need access to the manifest or whatever Rails stores the manifest as from within either my controller or within the view (inside an .erb tag).

How can I access a list of fingerprinted files? Or is there a better way of giving JavasScript access to asset paths that will only be known client-side.

Not entirely happy with this approach but here's what I have:

Note: The Gon gem to insert objects into the dom so that JavaScripts can access them.

In my controller:

gon.asset_host = Rails.configuration.action_controller.asset_host || ''
gon.manifest = Rails.configuration.assets.digests

In my JavaScript:

var manifest =  gon.manifest;
var fileName = panelName+'_'+layerName+'.png';
var image_path = 'resources/comics/'+comicName+'/'+fileName;
var hosted_path = image_path;
if(gon.asset_host) {
  var fingerprinted_path = manifest[image_path];
  hosted_path = gon.asset_host+'/assets/'+fingerprinted_path;
}else{
  hosted_path = '/assets/'+hosted_path;
}

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