So I deployed my RoR app on a droplet, which works find locally, the location of my application.js and application.scss is the following “app/javascript/packs/” Both are located here. But on my droplet, css and js is not working with the following error in the console of dev tools:
“Failed to load resource: the server responded with a status of 404 ()” “Failed to load resource: the server responded with a status of 404 ()”
“GET https://my_app/packs/css/application-988c2e01.css net::ERR_ABORTED 404” “GET https://my_app/packs/js/application-b5d6631ec473acfb5df1.js net::ERR_ABORTED 404”
Any idea on why is this happening and should I do to solve this.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hey!
The issue you’re facing with assets not being found in your Rails app on the droplet, while everything works fine locally, is a common issue related to asset compilation and serving in the production environment. Here are several steps and checks to ensure your assets are correctly compiled and served:
*: In the production environment, Rails doesn’t compile assets on the fly like it does in development. You need to precompile your assets. Run the following command in your app’s root directory on your droplet:
RAILS_ENV=production bundle exec rails assets:precompile
This command compiles all your assets, including JavaScript and CSS, and places them in the public/assets or public/packs directory, depending on whether you are using the webpacker or sprockets.
After precompilation, ensure that the public/packs directory contains your compiled assets. If the assets are not there, the precompilation might have failed, and you’ll need to check the logs for any errors.
If you’re using Webpacker, check your config/webpacker.yml file. Ensure that the settings under the production section are correct. Particularly, look for the public_output_path setting; it should typically be set to packs.
If you’re using an asset host or CDN, ensure that the config.action_controller.asset_host in your config/environments/production.rb is set correctly. If you’re not using an asset host, ensure this is either commented out or set appropriately.
Make sure that webpacker is installed correctly on the production server. You can run bundle exec rails webpacker:install to ensure it’s set up properly. However, be careful as this might overwrite some custom configurations.
After precompiling your assets, restart your Rails server to ensure it picks up the changes:
sudo systemctl restart your-rails-app-service
Replace your-rails-app-service with the actual service name if you’re using a system service to manage your Rails app. If not, just stop and start the Rails server manually.
If you’re using Nginx or Apache as a reverse proxy, make sure that the configuration is correctly serving your assets. For Nginx, you should have a location block that looks something like this:
location ~ ^/(assets|packs)/ {
root /path/to/your/app/public;
gzip_static on;
expires max;
add_header Cache-Control public;
}
Adjust the /path/to/your/app/public to the actual path of your app’s public directory.
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.