After successfully deploying my Rails application to an Ubuntu 16.04 Droplet, I encountered a problem with the CSS when running the server locally. The website could be accessed at myappname.com through the browser as long as the rails server was active, but once it was shut down, the CSS would fail.
Furthermore, the CSS consistently failed for anyone accessing the site from a device other than my local computer.
The root cause of this issue is due to me mistakenly hard coding the site to attempt to retrieve files from the local computer of whoever visits the site instead of the actual server. The error message highlights this problem:
http://localhost:3000/assets/jquery-ui/accordion.self-ahsdjhkjads98ha98shd8ha98hds98hadskjhfo4h8fw9hhw398hhiuh9sd8h8hs89fdhj.css?body=1 Failed to load resource: net::ERR_CONNECTION_REFUSED
Since "localhost" is mentioned in the URL, every visitor will try to load the page using something on their own local computer. In reality, it's my computer listening on port 3000 in this scenario, so I must adjust how the site code fetches CSS assets from itself rather than localhost.
In
config/environments/production.rb
, I made the following change:
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
to:
config.serve_static_files = true
Subsequently, on my Ubuntu server within the production environment, I executed
bundle exec rake assets:precompile
, restarted the server with sudo service apache2 restart
, yet the issue persisted. Any assistance provided would be greatly valued.