Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uninitialized constant AssetSync

I used the gem asset_sync and aws to precompile my assets. rake assets:precompile works fine. After I pushed my app to heroku, and

heroku run rake db:migrate

I get the following error

"uninitialized constant AssetSync"

initializers/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

config/production.rb

config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
config.assets.enabled = true 

After running run rake assets:precompile the first time, all my app/assets/images were moved to public/assets . I've deleted them from github and added public/assets/* to .gitignore. May this be the problem?

Edit: when running git push heroku master, it looks like they were precompiled

   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)
like image 463
vladCovaliov Avatar asked Nov 09 '12 13:11

vladCovaliov


1 Answers

Your initializer assumes that AssetSync is always defined, but this will not be the case if your Gemfile looks like:

group :assets do
  gem 'asset_sync'
end

The asset_sync documentation recommends wrapping the initializer in:

if defined?(AssetSync)
...
end

This is because Heroku runs production without the assets group of gems. Heroku precompiles your assets when you run a push -- and if asset_sync is enabled, it will update S3 at that time -- so when your application launches later, it no longer needs those gems. Thus, your asset_sync initializer needs to handle the situation where the gem is not loaded.

like image 67
willglynn Avatar answered Nov 18 '22 20:11

willglynn