Most of my assets suddenly return 404 after a push

2019-02-04 07:49发布

I have deployed this app (rails 3.2.11) a million times, I haven't messed with any settings, but now I'm greeted with this:

enter image description here

Why did this happen out of the blue? My conents of application.rb include config.assets.enabled = true - never had any issues.

In fact running it locally on port 3000 seems to not have any issues whatsoever.

After deploying to heroku this morning, it seems that it loads nothing that's inside /assets/

Interestingly, after copying the files over to try and just make a new app, git commit results in all the stuff you'd expect as well as a LONG list of these which I think might be related:

enter image description here

Edit: Interestingly enough SOME of the assets have loaded, like the logo and the background, but the rest as you can see return 404.

6条回答
贪生不怕死
2楼-- · 2019-02-04 08:12

Rails recommended that this setting config.serve_static_assets by default should be disabled i.e. set to false. Here is the default configuration in config/environments/production.rb generated in rails app

Disable Rails's static asset server (Apache or nginx will already do this)

config.serve_static_assets = false

So if you are setting it to true in your local app then that's still fine. But if you are deploying your app on Apache or ngix or anything other than heroku then its not advisable to make config.serve_static_assets=true in your production.rb config file. Here is the documentation from Rails guides.

config.serve_static_files configures Rails itself to serve static files. Defaults to true, but in the production environment is turned off as the server software (e.g. NGINX or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.

URL - http://guides.rubyonrails.org/configuring.html

查看更多
小情绪 Triste *
3楼-- · 2019-02-04 08:13

Heroku released a gem to handle the assets without needing to turn off compilation or to manually compile.

https://devcenter.heroku.com/articles/ruby-support#static-assets

Just add this to your Gemfile and redeploy.

gem 'rails_serve_static_assets', group: [:production]
查看更多
倾城 Initia
4楼-- · 2019-02-04 08:14

For Rails 4, use:

config.serve_static_assets = true

The default was false. We needed this after removing the rails_12factor gem.

查看更多
不美不萌又怎样
5楼-- · 2019-02-04 08:20

I had this problem today with rails 4 on heroku. The article provided by @Jeff is a little bit old but, the gem repository has a good readme. Summarizing, you will need to add two gems to your Gemfile:

  1. gem 'rails_serve_static_assets' (it will solve the static assets problem) and
  2. gem 'rails_stdout_logging' (which the previous one depends on).
查看更多
别忘想泡老子
6楼-- · 2019-02-04 08:33

put line in config/environments/production.rb

config.assets.compile = true

it worked as it will compile the assets on runtime, just like in development environment, but its makes application slow, the best way is to either compile the assets locally in production environment with rake task(RAILS_ENV=production bundle exec rake assets:precompile) and commit your generated assets in public/assets and then do deployment. or, heroku run rake assets:precompile

查看更多
▲ chillily
7楼-- · 2019-02-04 08:34

To make the assets to load with the corresponding fingerprint of each file verify the configuration config/environments/production.rb has the instruction:

ruby # Load assets with fingerprint behavior config.assets.digest = true

查看更多
登录 后发表回答