Failure to precompile assets when pushing rails app to Heroku - ruby-on-rails

I'm fairly new to rails and web development in general, so maybe I'm forgetting to do something simple, but I've been stuck on this problem for hours. I'm trying to push my rails app to Heroku, and I keep getting a message "Precompiling assets failed". Scrolling up through the console, the only place I really see errors is
Running: rake assets:precompile
remote: I, [2015-06-09T16:33:23.020943 #736] INFO -- : Writing /tmp/build_a3606aeafbcd156e85ce23a261b8ffd5/public/assets/logo-7f005e1459dfe2528e40b953dcc9b0d4.png
remote: PG::UndefinedTable: ERROR: relation "speed_records" does not exist
remote: LINE 1: SELECT "speed_records".* FROM "speed_records"
remote: ^
remote: : SELECT "speed_records".* FROM "speed_records"
remote: rake aborted!
remote: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "speed_records" does not exist
remote: LINE 1: SELECT "speed_records".* FROM "speed_records"
remote: ^
remote: : SELECT "speed_records".* FROM "speed_records"
remote: (in /tmp/build_a3606aeafbcd156e85ce23a261b8ffd5/app/assets/javascripts/googlemaps.js.erb)
remote: /tmp/build_a3606aeafbcd156e85ce23a261b8ffd5/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `async_exec'
This is a small project that I'm working with others on, and we're all deploying to separate Heroku apps to test our own work as one of the APIs that we're using doesn't work when run on localhost:3000. Other than that, the site runs fine on localhost, but Heroku won't allow me to push to it without errors. Even when I delete everything that I have locally, and clone the master branch from scratch, which is already hosted and working fine on a different Heroku, I still get an error when I try to push it myself. I think part of the issue could be related to the fact that we use the sqlite3 gem for development, but postgres for production, as Heroku requires. However I'm really new to working with databases so I have no idea if that is actually correct or if I'm forgetting to do anything.
Thanks in advance for any help, I've spent hours googling this issue and am completely stumped.
edit: found the solution after spending way too much time on it. The root of the problem seemed to be that I wasn't using "bundle exec" before all my attempted rake commands. From my understanding, this is necessary when the versions of gems you're using for a specific project don't match the versions you've installed system wide.

You need to run migrations. It looks like one of your .js.erb assets is loading some data from a database table that doesn't exist.
You can fix this by running rake db:migrate before you run rake assets:precompile, but you should be aware that your assets will not be dynamically recomputed once precompiled, and whatever data exists in the database at the point of precompilation is the data you'll be stuck using.

I am also faced this type of issue.
Open config/environments/production.rb and make sure the following option is set to true:
then please run
rake assets:precompile --trace RAILS_ENV=production

Related

heroku push of Rails 3.2 app suddenly fails with "Could not detect rake tasks" (no other errors)

I changed nothing in the configuration (no new gems or version changes); just one line of code, but I am now unable to push to heroku. Something must have changed at heroku?
It is Rails 3.2.22.5; Ruby 2.3.1. (Yes, I'm in the middle of upgrading to Rails 5, but it's a huge job, and in the meantime I still need to maintain the existing production system.)
RAILS_ENV=production bundle exec rake -P
works fine. I don't think it's really a rake issue. My best guess is something changed at heroku such that some version of something I have is no longer supported, but I have no idea what. And I'm not getting any informative error messages.
At a loss... I've Googled extensively, but most of the posts are 4+ years old. I tried precompiling locally and checking in only the manifest file. I tried upgrading rake (from 13.0.0 to 13.0.1), and have since put everything back since nothing worked.
remote: Bundle complete! 54 Gemfile dependencies, 118 gems now installed.
remote: Gems in the groups development and test were not installed.
remote: Bundled gems are installed into `./vendor/bundle`
remote: Bundle completed (0.51s)
remote: Cleaning up the bundler cache.
remote: -----> Writing config/database.yml to read from DATABASE_URL
remote: -----> Installing node-v12.16.2-linux-x64
remote: -----> Detecting rake tasks
remote:
remote: !
remote: ! Could not detect rake tasks
remote: ! ensure you can run `$ bundle exec rake -P` against your app
remote: ! and using the production group of your Gemfile.
remote: ! bash: /tmp/build_785c3bf77f08cff81f442938e3386876/bin/rake: /app/vendor/ruby-2.3.1/bin/ruby: bad interpreter: No such file or directory
remote: !
remote: /tmp/buildpackvv7B3/lib/language_pack/helpers/rake_runner.rb:106:in `load_rake_tasks!': Could not detect rake tasks (LanguagePack::Helpers::RakeRunner::CannotLoadRakefileError)
I asked heroku support, and they found the problem. Nothing to do with rake or my project itself, as expected (since I hadn't changed anything).
It had to do with the buildpacks. I need a PDF-generator, so I had a "multi-buildpack" set up (done years ago), apparently in a way that's no longer supported. I believe the proximate cause was pointing to a "master" buildpack, instead of the stable release. So somebody checked something in at heroku, and it broke my project :) It would probably only affect people who'd done set it up years ago, on the old cedar stack, as I did.
I ran:
heroku buildpacks:remove https://github.com/heroku/heroku-buildpack-multi.git
heroku buildpacks:add heroku/ruby
heroku buildpacks:add https://github.com/dscout/wkhtmltopdf-buildpack.git
and was able to deploy again!

heroku Uglifier::Error: Name expected

After try to deploy a simple rails test app into heroku, in the terminal poped up this
Uglifier::Error: Name expected
in the middle of the assets compilation.
i've searched and i did not find anything related to this, so someone ahs any idea this name expected is about?
remote: I, [2018-04-27T20:10:19.341334 #1686] INFO -- : Writing /tmp/build_171697f13e39a581026e766b205b8f86/public/assets/white-774a5ae6a58216b107623782ce2e1820ebb64f9d294cad32e132c086866f3dd2.png
remote: rake aborted!
remote: Uglifier::Error: Name expected
remote:
/tmp/build_171697f13e39a5810263766b205b8f86/vendor/bundle/ruby/2.3.0/gems/uglifier-4.1.4/lib/uglifier.rb:234:in `parse_result'
Make sure your asset pre-compilation works locally
bundle exec rake assets:precompile
If there are any errors that should help you find where they are happening.
Also see possible duplicate of Heroku Precompiling Assets Failed

[update]How to tell heroku not to precompile assets during the deployment stage (while pushing it to heroku)?

So i made this ruby on rails (rails 4) application with a lot of assets, images and css stylesheets, and pushed it onto heroku at first and it did push successfully but when i tried to run it my images all had a question mark so i did search up on this and came up with a solution where I had to change the configuration/production file's "config.assets.compile" from false to true and deployed it again and it worked no problem.
Then for my second iteration of the project I added bootstrap support into many of my css files and tried to deploy to heroku and i run into this
remote: /tmp/build_d1ed400bad4dd39eea2ab72184854c0e/vendor/bundle/ruby/2.2.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
remote: Tasks: TOP => assets:precompile
remote: (See full trace by running task with --trace)
remote: !
remote: ! Precompiling assets failed.
remote: !
remote:
remote: ! Push rejected, failed to compile Ruby app
remote:
Ive researched a lot on this and some of the methods to fix this have been depreciated but this one particular solution where it says to precompile the assets locally using
RAILS_ENV=production bundle exec rake assets:precompile
did successfully run locally and it create files under the public directory but when I deploy it to heroku it still precompiles the assets. So my question is: Is there a way to let heroku know that there are assets precompiled and there is no need to run that command again? or is there a better way to solve this?
The question is very broad, but give this a whirl and let me know if it works..
go into your config/environments/production file and look for
config.assets.compile = false
turn that to
config.assets.compile = true
I've had something similar happen after installing bootstrap and that was the fix.

Error: Sprockets::FileNotFound: couldn't find file 'application.scss' for Heroku

I'm having some issues with Sprockets not finding a file during rake assets:precompile. I haven't had this issue before, but when I added a generated migration (which built locally), Heroku failed to build my app, and I don't know why, because it builds locally (without me running an additional rake assets:precompile).
Error message is this:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
Sprockets::FileNotFound: could not find file: /tmp/build_dd4a799769e6963b7b292df72db58fd8/username-appname-uniqueid/app/assets/stylesheets/application.scss
Any help with why this is? Thank you for your time!
PS. As a side note - when I run "rake assets:precompile" and then push to Heroku, the app builds. However, an item with the css class "fa-bars" gets messed up... why is this?
Thanks!
I add the same issue with Sprockets 3.3.3 and our Rails 4.2.3 app.
I fixed it by cleaning our dyno build cache with:
heroku repo:purge_cache
Please note that your next deployment will take longer as bundler cache will be cleared too.
In rails 5 if you use Bower, uncomment in bower_rails.rb
#bower_rails.install_before_precompile = true

Rails 3 db:reset and assets

I made what I now see as a mistake (better now than later, not as good as never) of running rake db:reset on my local dev env to try to solve a problem I was having with assets production compilation(RAILS_ENV=production bundle exec rake assets:precompile) referring to a PG ERROR summarized bellow.
PGError: ERROR: relation "schools" does not exist
LINE 4: WHERE a.attrelid = '"schools"'::regclass
The tables get created properly and seeding works with exception of active_admin. I use Active_Admin that did not seed properly. My emails mysteriously stopped being sent and the assets compilation for production still fails with the same error. I am not sure what I can post here to help you understand the issue but if someone could help me tackle one at a time that would be much appreciated. Here are my issues in list form.
active_admin not seeding (I tried running: rails generate active_admin:install but that started to break things further so I reverted this.
PG ERROR on assets precompile for production persists.
Email stopped being processed (not using DJ)
Env.
Rails 3.1
------UPDATE 00------
2 is Solved. db:reset dropped all my tables but only migrated and seeded my development database so I also had to:
run migrate and seed for production ( rake db:migrate RAILS_ENV="production", rake db:seed RAILS_ENV="production")
before compiling production assets(rake db:migrate RAILS_ENV="production")
I ended up splitting this question into 2 parts. The second part got better contribution and is where I found the solution for this as well.
Detailed solution here

Resources