Getting Rail4 asset-pipeline to heroku - ruby-on-rails

My css & images are not making it to heroku. But I did what their tutorial said to do.
RAILS_ENV=production bundle exec rake assets:precompile
git add public/assets
git commit -m "assets added"
Then I heroku push and get
-----> Preparing app for Rails asset pipeline
Detected manifest file, assuming assets were compiled locally
so my assets pushed but there is still no css images or anything on my heroku app. Any ideas?

Try don't send your assets to heroku.
Remove public/assets from git:
git rm -r public/assets
git commit -m "Remove assets"
If still not working, on config/environments/production.rb set:
config.assets.compile = true
You also can try, on config/environments/production.rb set:
config.serve_static_assets = true

Heroku needs to have static assets, so each time you push to Heroku & change your assets, you should use this command:
rake assets:precompile

Related

Heroku is serving old assets for Rails 5 application

I've deployed a new version of a Rails 5 app on Heroku, running on cedar-14 stack. It didn't precompile while deploying, so I did heroku run rake assets:precompile manually. Still, I can see it includes old assets while requiring css and js files.
My files are in app/assets so it's not possible that directory isn't in assets compile path.
My config on application.rb and production.rb:
config.assets.compile = true
# I checked the environment variable, it responds to 'enabled',
# which would return true for the option.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Which I changed to expire old assets.
config.assets.version='1.1'
I tried these, but they didn't work:
$ heroku restart
$ heroku run rake assets:precompile
$ heroku run rake assets:clobber
The weird thing with these is that they do not affect assets in heroku server, which I checked with $ heroku run ls public/assets. Even after $ rake assets:precompile, even though it says this:
WRITING /app/public/assets/application-{VERY_LONG_HASH}.js
WRITING /app/public/assets/application-{VERY_LONG_HASH}.js.gz
WRITING /app/public/assets/application-{VERY_LONG_HASH}.css
WRITING /app/public/assets/application-{VERY_LONG_HASH}.css.gz
when I peek with the $ heroku run ls public/assets, I still see old assets staying there.
EDIT: I solved it by deleting all the local assets in public/assets, recompiling them with $ rake assets:clean && rake assets:precompile and including these assets in my git repository. Here is one concern:
Shouldn't heroku be responsible of compiling my assets on the fly? I think I shouldn't be compiling my assets everytime I deploy my application. Thanks.
Run on local
RAILS_ENV=production bundle exec rake assets:precompile
Next git add .
Next got commit -m"assets precompile"
Next git push origin yourBranchName
Deploy on heroku and you are done
You need to commit your changes first.
Then execute git push heroku yourbranch:master

Removing Precompiled Assets in Rails before Push to Heroku

I have a Heroku app on Rails 4.0.8.
At some point early on, I realized that my CSS and JS changes wouldn't push to Heroku unless I ran rake assets:precompile and committed the new asset files before pushing (git push heroku master).
I've been doing those asset precompiles for about 14 months now, and I've amassed over 48mb in what appear to be old precompiled assets. However, when I remove them from public/assets manually, my site loses all of its CSS and JS.
How do I remove the old precompiled assets and get Heroku to start compiling on push?
Follow these steps:
rake assets:clean
git add .
git commit -m commit_name
git push heroku branch_name
This will clean your assets locally + pushing on heroku would precompile the assets.
Try the following code.
rake assets:clean

Rails Asset Pipeline precompile, how to do it the right way?

I'm truely new on Rails 4.0. I don't understand the precompilation process when I'm pushing on heroku Cedar stack. I do this :
$> rake assests:precompile
$> git add .
$> git commit -m "foobar" && git push && git push heroku
All seems be working correctly, but my images/css don't seem to be "loaded" (Chrome) on the heroku app (but works perfectly locally) :
Failed to load resource: the server responded with a status of 404 (Not Found) http://xxxapp.herokuapp.com/stylesheets/bootstrap.min.css
I tried something, but I've heard that is was not the good solution. I have modified config/environments/production.rb file and changed :
config.assets.compile = false
to
config.assets.compile = true
It works fine, all my images and css are loaded, but I think I missed something.
Also
I tried :
$> rm -rf public/assets
$> git add .
$> git commit -m "foooo" && git push && git push heroku
The precompile process runs well but after, still no images and css loaded on my app.
After edited my Gemfile by adding :
group :production do
gem 'rails_12factor'
end
images are loaded well but no css..
Heroku should compile the assets for you by default.
You don't need to do anything else.
If you already compiled your assets, you may want to delete them from the public directory and then just push to heroku again.
You should precompile on heroku, not on your computer.
heroku run rake assets:precompile
I don't know, if this is the problem, I did this a year ago.

Is it possible to skip the asset precompile step for a single git push on Heroku?

Every time I deploy my Rails 3.2 project to Heroku, rake assets:precompile is run:
$ git push heroku master
...
----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
Asset precompilation completed (189.17s)
...
Sometimes I want to make a push that I know does not change any assets, such as a quick hotfix to a controller. Is it possible to skip the asset:precompile step for a single git push to Heroku?
Thanks.
Sure! You'll need to create a manifest.yml in your_app/pubilc/assets directory.
The file can be blank. But ideally, you precompile everything locally, so deploys to Heroku would be much faster.
Make sure that you also committed the manifest.yml file when you're pushing to Heroku. Something like git add -f your_app/pubilc/assets/manifest.yml and a git push heroku master should suffice.
This worked for me. manifest.yml did nothing for me on my rails 4 project.
https://gist.github.com/Geesu/d0b58488cfae51f361c6
Just precompile locally with rake assets: precompile, check in the resulting assets that are in public/assets, and push to heroku.
This will automatically create the manifest-.yml or json file in your public/assets directory; then heroku will detect that and report Detected manifest file, assuming assets were compiled locally.
Note 1: Some people have a line in development.rb that makes these go to public/dev-assets instead; if so, you need to rename dev-assets to just assets)
Note 2: Make sure your .gitignore file is not excluding the public/assets directory.
In rails 4, create the file manifest-<md5 hash>.json instead of manifest.yml

Confusion about rake assets:clean / cleanup on the asset pipeline in rails

Could somebody explain to me what the command rake assets:clean really does? Unfortunately the Rails Guides dont mention it. There is also the command rake assets:cleanup. Whats the difference?
Furthermore could somebody tell me when do I have to run rake assets:precompile in production. Do I run it on the server console after I deployed all my application files to my production server? Or do I precompile on my local machine and then do a deploy of all files?
Thanks all
Note: This answer is rails 3 specific. For rails 4 and later, look at other answers here.
If you precompile on your local machine, then you can commit these generated assets into the repository and proceed with deployment. No need to compile them on production machine.
But it introduces a problem: now when you change source files (coffescript / scss), the app won't pick up the changes, because it will serve precompiled files instead. rake assets:clean deletes these precompiled files.
In my projects assets are precompiled as a part of deployment. Capistrano makes it very easy.
Also, I never heard of rake assets:cleanup.
Run rake assets:clobber to actually clean the assets.
http://www.dixis.com/?p=735
Sergio's answer was completely correct in Rails 3. rake assets:clean deleted all assets that had been previously precompiled into the public/assets directory.
In Rails 4, you run rake assets:clobber to do the same thing.
If you run rake assets:precompile with the following config (by default turned on in staging and production):
# config/environments/production.rb
config.assets.digest = true
You compiled assets get timestamped. This means you can compile your new assets while leaving the old assets in place. You usually want to do this in production so you website will still access the old files while your running precompile to create your new files (because you've added new css/javascript). You now want to get rid of the old files that are no longer in use. The clean it removes the old versions of the precompiled assets while leaving the new assets in place.
rake assets:clean removes compiled assets. It is run by cap deploy:assets:clean to remove compiled assets, generally from a remote server.
cap deploy:clean removes old releases, generally from a remote server. It is not rake assets:clean
rake != cap
rake assets:clean is now run by cap deploy:cleanup_assets. Add require 'capistrano/rails/assets' to your Capfile and you get this cap-task. My capistrano version is v3.2.1.
clean up those untracked files with git clean -f for files and git clean -f -d for directories

Resources