Reputation: 325
Rails 4.1.1 production installation using Apache/Passenger configured to run rails app in a subdirectory "/myapp" instead of the root of the site.
All links in the app work fine, but in production references to assets in SCSS file, for example
background-image: image-url('logo.png');
get compiled to:
background-image: url("/assets/logo-<hash>.png");
instead of:
background-image: url("/myapp/assets/logo-<hash>.png");
I tried doing something like this when precompiling assets:
RAILS_ENV=production RAILS_RELATIVE_URL_ROOT=/myapp rake assets:precompile
but that had no effect.
I also tried setting config.relative_url_root
in the config/environments/production.env
file, with no luck.
The Rails guides say to set config.action_controller.relative_url_root
, but that results in a missing method error.
I eventually gave up and embedded the image in the CSS file by using:
background-image: asset-data-url('logo.png');
which works, but only by avoiding the subdirectory problem altogether.
I know Rails makes you pay for not following conventions, but having an app in a subdirectory is not exactly outlandish.
I've seem all kinds of posts on this problem, but it's hard to get a straight answer. It seems to have been fixed a few times for earlier versions of Rails, but it's not clear if the fixes ever got into the standard Rails distribution.
Upvotes: 1
Views: 2024
Reputation: 325
After adding the jquery-ui-rails gem to another app deployed in a subdirectory, I had the same problem with the assets referred to by the built-in CSS for jQuery UI. I refused to modify the 3rd-party CSS files just to fix this problem, especially since Rails seems to be handling all paths (other than asset paths) correctly out of the box.
After pulling my hair out for some time, I don't know what prompted me to re-run the rake assets:precompile task exactly as given here. To my complete surprise, everything worked perfectly, both my own image URLs and jQuery UI's.
What made the difference? If I call the rake script from the app's bin folder as shown below, all the asset URLs are built incorrectly, without the subdirectory.
RAILS_ENV=production RAILS_RELATIVE_URL_ROOT=/myapp bin/rake assets:precompile
If instead I run rake via bundle exec, as follows, all the asset paths are generated correctly, including the subdirectory.
RAILS_RELATIVE_URL_ROOT=/myapp RAILS_ENV=production bundle exec rake assets:precompile
As to why this is happening, I can only conclude that running bin/rake does not load the right gems.
Upvotes: 0
Reputation: 4296
In our case, we were able to make two changes, which seem to be working for us in Rails 4.1.6 :
1) In application.rb, assume there is a module named "MyAppName < Rails::Application". In here, we have
config.relative_url_root = "/myapp"
2) In config.ru:
map MyAppName::Application.config.relative_url_root || "/" do
run Rails.application
end
Without the config.ru update, things don't work properly.
Upvotes: 2