Question

Using Rails 4 on Heroku, precompiling assets and adding fingerprints.

I can access the assets from my erb files using:

<%= asset-url("image_name.jpg") %>

However, I'm making an ajax call to a controller to get a list of objects and I build an image name from each object, e.g. image-1.jpg, image-2.jpg, where the number here is from the javascript variable. These images are in the assets/images directory.

How can I link to them using the javascript variable, like this:

<%= asset-url("image-javascriptVariable.jpg") %>

This obviously doesn't work because it's rendered on the server well before the client renders the javascript variable.

Note: I know I could simply not preprocess/fingerprint these images and put them in public/assets and serve them static. However, I'd like to use the fingerprinting to break cache on updates.

Was it helpful?

Solution

You can't : erb/haml's interpolation run before rendering view (javascript here).

You should deport your logic in ruby in order to dynamically serve different assets files.

OTHER TIPS

If the amount of images is reasonable you can use ERB to build a data structure in Javascript (p.e. dictionary) of all images and pick the right image name later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top