1

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.

1

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Accepting this as the answer and it inspired me to think more correctly about Rails. I'm now rendering the asset_path in my controller/json output and sending that as a JSON value to the ajax request. Thanks.
0

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.

1 Comment

The image count is < 60 or so. I'm not sure exactly what you mean, could you elaborate with an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.