6

After deploying a Rails app to Heroku, no javascript functions are working.

The files appear to have been compiled (though its not easy to see in the minified file).

What is a logical process of steps to work out why the javascript is not working (it works fine in production).

Thanks

2
  • which version of rails and which heroku stack? if you're using rails 3.1+ i believe you should be on heroku cedar stack Commented Jun 8, 2012 at 9:02
  • Hi Dty. I'm using Rails 3.2 on Cedar with the Postgres 9 beta Commented Jun 8, 2012 at 9:19

2 Answers 2

6

This problem is associated with asset pipeline. You should compile assets.

To solve it, Turn config.assets.compress to true in config/environments/production.rb,

ie

  config.assets.compress = true

Then run RAILS_ENV=production bundle exec rake assets:precompile .

Push the code again.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks Shamith. I already have assets.compress=true in my configs. Heroku appears to be precompiling on deploy. At least, there is a log message to that effect. I will try your bundle exec suggestion though, and see how it goes.
Is it failing during slug compilation?
Just adding color to why this solution is necessary. From the Rails Asset Pipeline Documentation: "When using asset precompilation (the production default), you will need to ensure that your controller assets will be precompiled when loading them on a per page basis. By default .coffee and .scss files will not be precompiled on their own. " From: guides.rubyonrails.org/…
3

Sometimes I have issues because of the asset pipeline and my misunderstanding of it. So what I do just to make sure things are being packaged up correctly is just put in a simple alert when the page loads (put it on some random page users can't get to, etc)

alert('some-unique-string')

Push the code up to the server. Then in chrome bring up the page and use the dev tools, goto the "scripts" tab. From there you can search for the string some-unique-string since your string literal wont be minified. If you don't see that then you know your javascript isn't being included for some reason.

That at least will give you a starting point.

5 Comments

dty, the js is definitely being compiled, and alert('some-unique-string') is present in the source. But it is not running. Why would this be?
incidently, if I define a js function in the view file, it works. e,g, <script> alert('some-unique-string')</script>
ok, i think this is due to 2 issues. First, the order of scripts in application.js was generating an error that I hadn't picked up in dev environment. Second, I have on script that I don't want to include in the manifest. I am using it on a specific page, and it needs to go to the bottom of the page. I currently have this file in vendor/assets, and using javascript_include_tag on deployment I get a template error, js not precompiled. Where should this file be located, and how should I call it?
Ok, I eventually hacked my way around this. You were right, the issue was my understandings and assumptions about asset pipeline. Confused, but starting to see the light :) Thanks!
I was having similar issues and tried this. The alert popped and then everything else also worked. I deleted the alert and everything still worked. Not sure why. Something to do with how heroku compiles maybe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.