Rails Asset Pipeline precompile, how to do it the right way? - ruby-on-rails

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.

Related

Rails 6 deployed on Heroku won't update Stylesheets

I built a Rails 6 app and deployed it to Heroku. But any changes I make to the stylesheet are not reflected. All the Heroku documentation and SO questions/answers appear to no longer be relevant to the current Rails setup in this regard. I could precompile the assets before pushing to heroku but I'd prefer not to. And actually I did find a "solution" but it feels more like a hack than a real solution. If I open config/initializers/assets.rb and change the statement:
Rails.application.config.assets.version = '1.0'
to
Rails.application.config.assets.version = '1.1'
then it will update the assets. But that means if I am experimenting with the look of the site I would be changing the version all the time. I mean if that's they way it's supposed to work I'll live with it, but it doesn't seem right. Anyone know a way to get Heroku to just update it on every push?
You've to compile the assets from the heroku end(i.e. in your server on Heroku), so after pushing your code to Heroku, run:
$ heroku run rake assets:precompile
# If above doesn't work
$ heroku run RAILS_ENV=production rake assets:precompile
Also, it's a good practice to add public/assets(or whatever folder that precompiles to) folder to your .gitignore file, so incase if you precompile assets in your local environment this will not mess up with your production env when you push to Heroku. But everytime you change some CSS and push to heroku, you'll need to precomile assets on Heroku end from the above command.

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

Heroku not reflecting codes pushed to it and differs from Localhost

I have done up a web app with Ruby on Rails and made some changes on my codes in my computer. Thereafter, I did git add ., git commit, git push and git push heroku master.
The Localhost:3000 webpage reflected the codes and showed the change but on heroku, the webpage did not reflect the codes/change.
How do I resolve this?
did you precompile? Otherwise you won't see css changes.
RAILS_ENV=production bundle exec rake assets:precompile
Also see https://devcenter.heroku.com/articles/rails-asset-pipeline

Getting Rail4 asset-pipeline to heroku

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

Automatically precompile assets before pushing to Heroku

Is it possible to automatically precompile my assets in a Rails app before pushing out to Heroku? I always forget to do it, so it would be nice if when I typed git push heroku master, it would first run rake assets:precompile ; git commit add . ; git commit -a -m "Precompile.", or something to that effect.
Has anyone achieved such a solution? Possibly without hooks? Though I suspect that is the only way.
I finally figured this out. I was indeed on the Cedar stack. The problem was that I had checked my public directory into Git, and when I pushed to Heroku, it realized that public existed, and thus assumed I precompiled. Running git rm -r public and adding public/** to my .gitignore, then pushing, fixes the problem.
It sounds like you might not be on Heroku's Cedar Stack? If you're using the asset pipeline (Rails -v >= 3.1), cedar provides three options for compiling assets.
From the docs:
If you have not compiled assets locally, we will attempt to run the
assets:precompile task during slug compilation.
You could always alias heroku or something similar to rake assets:precompile ; git commit add . ; git commit -a -m "Precompile." ; git push heroku master in your bash profile
ie
#in ~/.bash_profile
alias precompile_push='rake assets:precompile ; git commit add . ; git commit -a -m "Precompile." ; git push heroku master'
On the cedar stack, it will do this during slug compilation. I recommend that.
I've created a gem that run as daemon and automatically pull changes from a Git repo, precompiles assets, commit and push back.
https://github.com/nectify/rails-precompile2git/

Resources