Ruby on Rails资产未在Heroku上进行预编译
我有一个可以在本地开发环境中工作的项目,但是当它上传到Heroku时就中断了.在Heroku上访问我的项目时,我注意到从服务器收到404响应,说它找不到我的css
和js
文件.我进行了一些搜索,发现Heroku没有预编译我的资产.在Heroku让我的项目进入睡眠状态之前,该项目将正常运行.在Heroku中唤醒项目后,css
和js
会断开.
I have a project that works in the local development environment but breaks when it is uploaded to Heroku. When visiting my project on Heroku, I notice that I get 404 responses from the server saying that it could not find my css
and js
files. I have done some searching and found out that Heroku is not precompiling my assets. The project will work fine until Heroku puts my project to sleep. Upon waking the project in Heroku, the css
and js
are broken.
该项目使用的是Rails 4.2.4,我确保在我的Gemfile中的config/application.rb
和gem 'rails_12factor', group: :production
中都包含了config.serve_static_assets = true
.
The project is using Rails 4.2.4, I have made sure to to include config.serve_static_assets = true
in my config/application.rb
and gem 'rails_12factor', group: :production
in my Gemfile.
css
和js
仅在Heroku由于不活动而使项目进入睡眠状态时才会中断.有谁知道从睡眠中唤醒后Heroku如何自动预编译资产?
The css
and js
only breaks when Heroku puts the project to sleep due to inactivity. Does anyone know how to have Heroku automatically precompile assets when it is awaken from sleep?
Gemfile
gem 'rails_12factor', group: :production
application.rb
默认情况下,Rails 4将不会为您的资产服务.要启用此功能,您需要进入config/application.rb并添加以下行:
application.rb
By default Rails 4 will not serve your assets. To enable this functionality you need to go into config/application.rb and add this line:
config.serve_static_assets = true
production.rb
config.serve_static_files = true
config.assets.compile = true
命令行
bundle install
bundle exec rake assets:precompile RAILS_ENV=production
确保图像位于/public文件夹中.
Make sure the images are in the /public folder.