2

Rails adds an md5 hash to images names when moving them to the /public/assets directory during precompilation. The problem is that these hashes are unpredictable, so how can I know what they're going to be called when trying to link to them?

For example, if I'm hosting an image named flowers.jpg, and then try to access it at www.mysite.com/flowers.jpg, it fails, because the file has been renamed flowers-4182172ae014ec23dc02739229c08dcc.

I know Rails has helpers that will automatically find these images. But what if you're trying to link to these images from a completely different website or application? Is there a way to get Rails to say, "Well I can't find a precompiled version of flowers.jpg, so instead of serving from /public/assets I'll serve from /app/assets."?

EDIT: According to this post (http://stackoverflow.com/questions/10045892/rails-compiles-assets-both-with-and-without-md5-hash-why), Rails should be compiling a version of my assets both with and WITHOUT an md5 hash? Any idea why my copy of Rails isn't generating a version without a fingerprint?

NudeCanalTroll
  • 2,266
  • 2
  • 19
  • 43

2 Answers2

0

Rails handle that for you using image_tag:

image_tag "myimage.jpg"

and this will get you the right url for it. You might be able to write a small service that generates the image url for your as (untested):

Class AssetsService < ApplicationController  
  def index
  end
end

index.js.haml

= image_tag "myimage.jpg"
Tam
  • 11,872
  • 19
  • 69
  • 119
0

The answer wasn't with Rails. I don't think Rails is supposed to compile images without the fingerprint. It's supposed to still be able to serve them, however, and I had added some code to my nginx config file that prevented this from happening. This was the offending code:

location ~* ^/assets/ {
    # Per RFC2616 - 1 year maximum expiry
    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
}
NudeCanalTroll
  • 2,266
  • 2
  • 19
  • 43