Heroku doesn't find assets - ruby-on-rails

I've tried every troubleshooting *including StackOverflow and Heroku documentations.
Tried to precompile all assets locally bundle exec rails_env=production rake assets:precompile
Added config.serve_static_assets = true in application.rb, also tried config.serve_static_files = true
Tried gem 'rails_12factor', group: :production
And other things, still didn't work. The website loads, without any assets, javascript or styles. The browser console gives this warning:
Loading failed for the with source “...
/assets/application-c2decc28eb82aba39474d1a38f2cc1f016634981895dfbe9cb87ebb12709630d.js”.
I have know idea of what can be. Is Heroku looking for assets somewhere else? How can I check it and fix it? It was working yesterday, but don't know what is causing this issue. Thank you very much.

The problem was on my environments/production.rb file.
This config was pointing to another host. Now fixed.
config.action_controller.asset_host = "https://yourhost.herokuapp.com/"

Related

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 not being properly precompiled by heroku

My assets are not being properly precompiled to my staging heroku app. I usually use rake assets:precompile before pushing but my senior developer told me not to use that and put /public/assets into gitignore saying that heroku will automatically precompile the assets. When I push, heroku says that it runs rake assets:precompile but none of my assets show up. The page is just html.
possible causes: before my senior developer told me his way, I ran rake assets:precompile and removed /public/assets from gitignore as I though it wasn't suppose to be there. But after he told me, I put /public/assets back into .gitignore and deleted all the public assets and the assets still are not working on the heroku staging app. What am I missing?
I found the solution here: https://stackoverflow.com/a/16571492/1890135
I didn't specify this in the question but I was making a 2nd staging app not called staging so I had to add the rails_12factor gem under this new staging name environment.
group :staging_new, :staging, :production do
gem 'rails_12factor' # To enable features such as static asset serving and logging on Heroku
end
Why did you include public/assets in your gitignore file anyway? There's no need for it to be in there, unless you've precompiled your assets locally & don't want to use them in Heroku?
--
A good way to test whether your app will work in staging is to run rake assets:precompile RAILS_ENV=production. This will do as Heroku would -- compiling your assets
What I would do initially is:
Precompile your assets locally (using rake assets:precompile RAILS_ENV=production)
Check in your public/assets folder for the precompiled files
If they're there, push to Heroku & do again
If not, there will be another problem to fix
Generally, though, Heroku's precompilation process is just the same as you would do locally. So maybe you could even try running your Rails server in production mode to see what the issue might be; it's probably more your app than Heroku

unable to display images on heroku

When i run the website locally, it works fine.However, after pushing to heroku, its displaying as a broken image
The pictures are in my /assets/images/ folder
images are called in my static page like this
<img src="assets/blank_avatar_male.jpg" alt="blank_male_avatar">
Ive tried changing the path to /assets/images/jpeg and it and repushed. No luck so far.
Any suggestions on how to fix this?
My guess is that you don't have config.serve_static_assets set to true in your production.rb file.
The simplest solution is to include the rails_12factor gem which will do this for you
try compiling your assets locally like this:
RAILS_ENV=production bundle exec rake assets:precompile
and then push to heroku

Ruby on Rails: Ran rake assets:precompile and now both local and heroku deployment don't include bootstrap

I was having issues deploying my project to a heroku server (Precompile fail). So I found this response, https://stackoverflow.com/a/13713753/2989437, and followed up on the advice. I added one line to my application.rb file:
application.rb
module FirstEdc
class Application < Rails::Application
config.assets.initialize_on_precompile = false # I added this line
...
end
end
I then ran the precompile command, committed the changes, and managed to deploy successfully to heroku. However, now my bootstrap/css appears to have stopped functioning both on the heroku deployment, and my local deployment.
I learned that I was supposed to add another line to my deployment.rb file:
deployment.rb
FirstEdc::Application.configure do
...
# Allows for local precompilling --added by Ian
config.assets.prefix = '/dev-assets'
end
So I added this, recompiled and redeployed, but to no avail.
Finally, I ran a rake assets:clean in an attempt to at least get my local deployment back to normal, but it did not work.
Any advice would be greatly appreciated. I'm reading more into the asset pipeline now, but I feel like this could be a cache problem or something. I'll update as I figure out what's going on.
edit. Just to clarify, I've tried removing both additions, running a rake assets:clean and rake assets:clean:all, but neither fix my local deployment.
I don't use config.assets.prefix in any of my apps and they work just fine with Heroku, so not sure what that's doing.
Try removing that line, then running rake assets:clean. Now your local server should be using the files as you change them. When you want to push, run rake assets:precompile first, then push.
If you want to make changes locally after that, run rake assets:clean again to get rid of the precompiled files on your local machine.
If Heroku detects any files in public/assets it will not attempt to precompile your assets again. This is by design.
So, you need to make a decision to either always precompile your assets with rake assets:precompile, or remove any files in public/assets before pushing to Heroku.
(The recommended way is to allow Heroku to precompile them during push)

Heroku doesn't update assets modifications

I'm using heroku to deploy my app in ruby on rails, but have a problem, when I deploy my app, heroku doesn't update with the modifications I made, before I compiled my assets. I solved my problem in localhost using a simple rake assets:clean but in heroku this doesn't solve what I make to back update when I modify my assets
config.assets.initialize_on_precompile = false
config.assets.initialize_on_precompile = true
Changed this but it doesn't work
There's a few things to do. The first is to redeploy
$ rake assets:clean
$ rake assets:precompile
$ git add .
... deploy to heroku
If it still doesnt work, it could be a caching issue, in which case, you can manually bump the version in config/application.rb and redeploy to expire all the previous assets
Ex:
config.assets.version = '1.2' # change to '1.3'
First of all. Heroku will pre-compile the assets for you when you deploy the app without assets as it will not find the manifest.yml. in assets.
To Make this work.
Remove all the assets files from the public folder in from local and heroku.
In production.rb
Make sure you have these code.
config.serve_static_assets = false
config.assets.compress = true
config.assets.digest = true
config.assets.compile = false
Set the assets.compile = true if you want the rails app to find asset for you if any asset is found. in precompiled. Ideally this option should be false.
config.assets.compile = true
Push these clean changes to heroku.
I had this same issue late last night, i found that after pushing to heroku it was disconnecting from git. Once I went through the GIT process again then pushed it to heroku everything updated. Not sure why it disconnects, but this has been my work around all day. Spent 2-3 hours trying to fix the root issue, but the work around is just faster.

Resources