我使用gem asset_sync和aws来预编译我的资产。 rake资产:预编译工作正常。我将我的应用程序推送到heroku后,
heroku run rake db:migrate
我收到以下错误
“未初始化的常量AssetSync”
初始化/ asset_sync.rb
AssetSync.configure do |config|
config.fog_provider = 'AWS'
config.aws_access_key_id = "..."
config.aws_secret_access_key = "..."
config.fog_directory = Rails.env + "-..."
config.fog_region = 'eu-west-1'
end
配置/ production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
config.assets.enabled = true
运行rake资产后:第一次预编译,我的所有app / assets / images都被移动到public / assets。我已经从github删除了它们并将public / assets / *添加到.gitignore。这可能是问题吗?
编辑:当运行git push heroku master时,看起来它们是预编译的
Preparing app for Rails asset pipeline
Running: rake assets:precompile
AssetSync: using /tmp/build_2ltvklj0gaxjp/config/initializers/asset_sync.rb
AssetSync: using /tmp/build_2ltvklj0gaxjp/config/initializers/asset_sync.rb
AssetSync: Syncing.
Using: Directory Search of /tmp/build_2ltvklj0gaxjp/public/assets
Uploading: assets/application-7e17d9f0ed9cb7ea50b750e2bfc7e28c.css.gz
Uploading: assets/application-7e17d9f0ed9cb7ea50b750e2bfc7e28c.css
AssetSync: Done.
Asset precompilation completed (58.04s)
答案 0 :(得分:13)
您的初始化程序假定始终定义了AssetSync,但如果您的Gemfile
看起来不是这样的话,那么情况并非如此:
group :assets do
gem 'asset_sync'
end
asset_sync
文档建议将初始化程序包装在:
if defined?(AssetSync)
...
end
这是因为Heroku在没有production
组宝石的情况下运行assets
。 Heroku在您运行推送时预编译您的资产 - 如果启用asset_sync
,它将在那时更新S3 - 因此当您的应用程序稍后启动时,它不再需要这些宝石。因此,您的asset_sync
初始化程序需要处理未加载gem的情况。