Home > Mobile >  Rails Production not appending relative_url_root to image
Rails Production not appending relative_url_root to image

Time:12-17

I have app foobar that has a relative path. For example: https://my_main_app.com/foobar

In config/environments/production.rb I account for this with this setting:

config.relative_url_root = '/foobar'

My css, js, and image assets are in the assets directory:

 -- app
|   -- assets
   |   -- images
      |   -- my_img.jpg
   |   -- javascripts
      |   -- application.js
   |   -- stylesheets
      |   -- application.css.scss

I run rake assets:precompile RAILS_ENV=production and everything appears to compile ok. The css, javascript, and images all get put into the public/assets directory with an appended digest:

.
|   -- app
|   -- public
   |   -- assets
      |   -- application-<digest1234>.css.gz
      |   -- application-<digest3456>.js.gz
      |   -- my_img-<digest5678>.jpg

When I access the app:

  • The css works great
  • The js works great
  • The image is 404 not found.

Failed to load resource: the server responded with a status of 404 (Not Found) https://my_main_app.com/assets/my_img-.jpg

I know what the issue is: the issue is that for the image: it is not appending the relative path of foobar. The correct path to the image is:

https://my_main_app.com/foobar/assets/my_img-<digest5678>.jpg

Other detail:

I am loading the file like so. Within app/views/layouts/application.html.erb I have this line:

<div id="header"></div>

Then in app/assets/stylesheets/application.css.scss I have this line:

#header {
  background-image: image-url('my_img.jpg');
  background-repeat: no-repeat;
  height: 100px;
  background-size: 100%;
}

So it appears that when the assets are compiled, it doesn't know about the relative path.

How do I fix this?

CodePudding user response:

I got it working but I did a number of things beforehand. It is unclear to me what exactly did the trick. Here are the things I know I did:

I completely cleared both my cookies and my cache.

I deleted the images from public/assets

I restarted the application server (passenger).

I recompiled the assets, but I made a few tiny adjustments to the command this time (used rails instead of rake and I specified the environment variable in the front).

RAILS_ENV=production rails assets:precompile

It is unclear to me what exactly did the trick, but I am putting this here in case anyone runs into the same issue.

  • Related