Question

I'm trying to access a file logo.png from my rails app. During the deploy process everything in /app/assets gets compiled and placed into /public/assets. For example my file which had been named logo.png looks like this:

/public/assets/logo-e66eddecdb08ac2b7fe349da2a065d87.png

When I try to access that full filename nginx successfully serves it up directly because of these lines in my /etc/sites-enabled/myapp:

try_files $uri/index.html $uri @unicorn;
root /home/deployer/apps/myapp/current/public

So I can browse to myserverhost.com/assets/logo-e66eddecdb08ac2b7fe349da2a065d87.png and it loads fine. However of course in my app I have that file referenced simply as "logo.png" and when I load my home page I'm getting a 404 not found on the logo.png.

How is nginx supposed to know that the filename has been mashed up with a hash at the end?

EDIT: I reference the logo file like this in my scss file:

#sidebar .logo-div {
  border-bottom: 2px solid#ddd;
  width:100%;
  height:80px;
  text-align:center;
  background-image:url('logo.png');
  background-position:center;
  background-repeat:no-repeat;
  margin-bottom:20px;
}
Was it helpful?

Solution

You'll have to use an url helper.

background-image: url(image-path("logo.png"));

or asset-url("logo.png", image)

Rails automatically adds the hash in production.

OTHER TIPS

If your file is a css file:

background-image: url(/assets/logo.png);

If your file is a sass file can use the helper:

background-image: image-url("logo.png");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top