Uninitialized constant AssetSync - ruby-on-rails

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)

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.

Related

Rails: How do I force a new asset digest on ALL assets?

Running a rails 4.2 app with sprockets and using asset digests. When running rake assets:precompile it creates all my assets with digests. I need however to bump all asset digests to help me debug some caching stuff in production. I tried changing the assets version in:
# config/initializers/assets.rb
Rails.application.config.assets.version = '6.4'
However running rake assets:precompile again after this is done, does not create new files with new digests. Shouldn't it? Or am I missing out on something?
I found the only way to force expiration of assets and get them recompiled was to add the following in my config/environments/production.rb
config.assets.version = '1.1' #This currently doesnt work as intended so use
config.assets.prefix = '/production'
Then bundle exec rake assets:precompile RAILS_ENV=production
Rails 4 and Sprockets 3 don't quite get along as per the thread here thus the versioning not working as intended: https://github.com/rails/sprockets-rails/issues/240

Ruby on Rails assets not being precompiled on Heroku

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.
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.
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?
I had similar issues before, my best bet was to precompile in local and then push to heroku. Configure your production.rb as follows:
config.serve_static_files = false
config.assets.compile = false
then in your console precompile as follows:
rake assets:precompile RAILS_ENV=production
This will precompile everything in public/assets commit your changes and push to heroku.
Also reset your assets cache for avoid any inconsistence:
rake assets:precompile RAILS_ENV=production
The above will force all your public/assets directory to rebuild when you run precompile command.
If your issue is with assets recompilation my answer should solve it, if you still have issues then you are doing something wrong or the issue does not have anything to do with assets precompilation.
We set the configuration values of above to false because now you are sending the precompiled files to the repo, so we do not serve static files nor fallback assets pipeline if something is missing, we are going everything in local.
Gemfile
gem 'rails_12factor', group: :production
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
Command Line
bundle install
bundle exec rake assets:precompile RAILS_ENV=production
Make sure the images are in the /public folder.
Reference here

Rails: assets unavailable in staging environment

I'm trying to setup a staging environment.
I copied the environments/production.rb to create environments/staging.rb.
I tested the staging environment locally and ran
RAILS_ENV=staging rake assets:clean assets:precompile
RAILS_ENV=staging rails s
Assets are properly generated in public/assets, and when I load a page, the links to the assets are correct (eg: /assets/application-e014056a81279320a97c71ed25eb46ab.css)
But the browser can't load them and if I try to open
http://localhost:3000/assets/application-e014056a81279320a97c71ed25eb46ab.css
I get a 404.
Now the strange thing is that it works in production, with:
RAILS_ENV=production rake assets:clean assets:precompile
RAILS_ENV=production rails s
For both environments/staging.rb and environments/production.rb, the config is
config.serve_static_assets = false
config.assets.compile = false
config.assets.digest = true
And in application.rb
Bundler.require(*Rails.groups(assets: %w(development test)))
Do you have any idea where to look ? What else could differentiate the staging environment from the production environment ?
So, I had forgotten the role of the 'rails_12factor' gem.
I updated my Gemfile to
gem 'rails_12factor', group: [:staging, :production]
And everything works now.

Rails Assets Precompile just not working

I've pushed a Rails app to Heroku and keep on running into the following problem:
I'll save changes to my main css.scss file (in assets/stylesheets) or to images in assets/images, push to git, push that to heroku, and reload the page, only to find out that these assets haven't been loaded at all.
This was also a slight problem on the local server, but entering:
rake assets:precompile
and reloading the local server usually worked, whereas doing
heroku run rake assets:precompile
and then re-pushing does nothing. I've fished around for info and haven't found anything particularly helpful.
Of note, in my config/application.rb (some of these the result of said fishing around):
# Enable the asset pipeline
config.assets.enabled = true
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
in config/environments/production.rb:
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
Of additional possible interest, when I push to heroku, it says, among other things, this:
Preparing app for Rails asset pipeline
Detected manifest.yml, assuming assets were compiled locally
-----> Rails plugin injection
Injecting rails_log_stdout
Injecting rails3_serve_static_assets
and
Installing dependencies using Bundler version 1.3.0.pre.5
Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
I learned with Rails 3 and don't really know how the assets pipeline differs from what was available in previous version, so sorry if I'm being an idiot and putting overlapping and/or contradictory settings in my config files.
Would appreciate any help. This has been a headache.
It looks like it could be that you are add your locally compiled assets to git and pushing them and as a result Heroku is not compiling your assets on push. Check to make sure you are not adding the public/assets directory to git.

rails assets:precompile during slug for s3 error: Fog provider and directory can't be blank when env are set

I decided to serve rails assets via S3; heroku has great tutorials on how to do this. The site is now serving assets from my amazon bucket but I'm unsure why I had to manually run heroku run rake assets:precompile after a git push heroku master which runs a rake assets:precompile.
After running the git push heroku master the assets where not in my bucket and the output for the precompile stuff was:
AssetSync: using default configuration from built-in initializer
AssetSync: using default configuration from built-in initializer
rake aborted!
Fog provider can't be blank, Fog directory can't be blank
/tmp/build_3vtwfg15g8ajx/vendor/bundle/ruby/1.9.1/gems/asset_sync-0.5.0/lib/asset_sync/asset_sync.rb:29:in `sync'
/tmp/build_3vtwfg15g8ajx/vendor/bundle/ruby/1.9.1/gems/asset_sync-0.5.0/lib/tasks/asset_sync.rake:3:in `block in <top (required)>'
Tasks: TOP => assets:precompile:nondigest
(See full trace by running task with --trace)
Precompiling assets failed, enabling runtime asset compilation
Injecting rails31_enable_runtime_asset_compilation
I did set the fog provider and directory with: heroku config:add FOG_DIRECTORY=XXX FOD_PROVIDER=AWS and calling heroku config --app confirms this...so I don't get those errors.
The assets didn't show up in my bucket so I ran: heroku run rake assets:precompile and everything worked with a warning:
AssetSync: using default configuration from built-in initializer
AssetSync: Syncing.
[WARNING] fog: the specified s3 bucket name(ss_assets) is not a valid dns name, which will negatively impact performance. For details see: http://docs.amazonwebservices.com/AmazonS3/latest/dev/Bucket
Restrictions.html
Will I always have to run the precompile task after and just be okay with the push failure? I'll check to see if the Warning of the directory name is causing the blank FOG errors on push
EDIT
Again asset_sync doesn't appear to have ENV variables when called in the assets:precompile task of the heroku push. Running that task after push works but it 'annoying'.
Still not working for me, latest attempt was (per asset_sync github project):
lib/tasks/asset_sync.rake.
Rake::Task['assets:precompile'].enhance do
AssetSync.sync
end
Rake::Task["assets:precompile:nondigest"].enhance do
AssetSync.sync
end
I also attempted adding lines to my production.rb file such as:
config.asset_sync.aws_bucket = ENV['FOG_DIRECTORY']
config.asset_sync.fog_provider = ENV['FOG_PROVIDER']
Didn't work for me either.
run below from asset_sync docs Labs section
heroku labs:enable user-env-compile -a myapp
Hasn't made it's way into the platform as standard yet!

Resources