Why is javascript not working on deployment to heroku? - ruby-on-rails

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

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.

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.

Related

Rails: Precompile for Heroku?

Before deploying a new version of my app on Heroku, I need to do this in my console (for css and js to work on Heroku): RAILS_ENV=production bundle exec rake assets:precompile.
I just picket this code line from a forum, and I my questions is:
1) Why do I need to do this?
2) Is it possible to implement something more permanent in my Rails code so it does this precompiling automatically (so I don't need to write it manually every time I do some changes in my css or js files)?
1) Why do I need to do this?
There are lot of js files and css file in rails app generally. The above command compress and minified all those files which will reduce the number of requests that browser makes to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer requests can mean faster loading for your application.
More
2) Is it possible to implement something more permanent in my Rails code so it does this precompiling automatically (so I don't need to write it manually every time I do some changes in my css or js files)?
Yes. Heroku automatically compiles your assets if you dont include public/assets/manifest.yml for rails 3 and in rails 4 it is public/assets/manifest-<md5 hash>.json
More
Related Answer : Automatically precompile assets before pushing to Heroku
In your production.rb
config.assets.compile = true

Javascript library not working on heroku

I'm using parsley.js in a rails project and it works perfectly fine on my local machine. On Heroku, it's as if it's not there. The javascript file is in app/assets. I looked at the compiled js that is served on heroku and parsley is definitely in there, but I'm not seeing it actually working.
I'm not even sure how to start troubleshooting this. What could be the issue?
I have the same problem before. But I make some changes in enviroments/production.rb file. First, try to add this line:
config.assets.compile = true
This should be enough. But if you hold your asset files in special locations, you may also add the line like this: config.assets.precompile += %w(ckeditor/*), where you should specify your locations.

rake assets:precompile every time I edit CSS?

When I looked at the HTML source for my main page, the CSS that Rails was linking to was in /stylesheets/application.css. When I tried to click that link, I would get a 404 not found error from Passenger (Apache).
So what ended up working was setting config.assets.compile = true in my config file and running rake assets:precompile. This then changed the CSS link to something like /assets/application-5310fa2adccd74453c084cf221eaeeef.css, and this was something I could click on and could be found.
So now what I'm doing is every time I edit my CSS file, I run precompile. Is this the correct way to do things? Do I really have to call this every time I edit my CSS file? Is this the purpose of precompiling, to make sure my assets are able to be served by Apache?
When you are running your application in production mode, it is advised to precompile the static assets to enhance the performance.
But if you want to skip it, you can use
In config/environmets/production.rb
config.assets.compile = true
After this, you don't need to precompile assets after any change in css but it can slow down the performance and speed of your page load.

Use assets from gem in production mode

I am trying to figure out how to get boostrap-sass working in production mode. I am using apache to reverse proxy to either webrick or puma, but serve the static assets in public/assets directly. When I precompile assets, the bootstrap css gets included into the application-(hash).css and it works correctly.
However the compiled css references an image file (glyphicons-halfling.png) without appending the hash of the file contents. The image file is included in public/assets directory, and it is possible to browse to it by putting the correct filename in the address bar, but the filename in the css does not match it. I have created a simple demo app that demonstrates this problem, code is on my github page
The glyphicon filename is glyphicons-halflings-c806376f05e4ccabe2c5315a8e95667c.png
[EDIT]
Would still like an answer to this question, but i've just renamed the offending files to remove the hash. Since these files are unlikely to change frequently then this should work fine
Think I have it cracked, when you run rake assets:precompile, it seems that you must prefix it with RAILS_ENV=production in order for it to work properly in production mode (I guess that kind of makes sense). If you don't, some of your assets will get precompiled, but the helper methods will not generate the correct paths.
tl:dr, RAILS_ENV=production rake assets:precompile

Rails 3.1 Assets are being fingerprinted in production but rendered HTML is not

Everything works great in Development. And app deploys as normal with Capistrano. Assets (javascript & css) appear to be fully pre-compiled and each, along with images, are given a "fingerprint". Problem is when using image_tag("image-name.png") in my view the html it creates in production doesn't include the 'fingerprint'.
The rendered HTML we get in production:
<img alt="Image-name" src="/assets/image-name.png" />
instead of, what I would expect, should be:
<img alt="Image-name" src="/assets/image-name-b89cae2830c2901f844c353b3f3942fe.png" />
So which of Rails 3.1's myriad config options did we botch?
Edit
The troublesome images appear to be those included in a 3rd-party Colorbox image viewing tool we use. Rails 3.1 is fingerprinting its assets (border.png, etc.) but, clearly, the source code for this javascript library doesn't use helpers like image_tag. So in production it is still looking for images named /assets/colorbox/border.png. Currently images are in /vendor/assets/images and work fine in Development. Is there a way to prevent just these images from being "fingerprinted"?
Well, here's how I hacked the 3rd-party files to make things work:
The offending images were in files like vendor/assets/stylesheets/colorbox.css. I first changed the extension of the files to .scss then I changed each url(colorbox/image.png) to image_url("color box/image.png") and now everything is peachy. Assets are served normally in development and fingerprinted in production.
Still like to see the "proper" way to add 3rd-party (vendor) javascript libraries & css to a Rails 3.1 app. Rails team must have anticipate a drop-in solution that doesn't require editing?!? So, please, feel free to offer up other solutions.
Aside: where I previously had manually configured my Capistrano recipe with:
run "cd #{release_path}; RAILS_ENV=production bundle exec rake assets:precompile"
…and its accompanying after deploy:update_code …. I have now removed those lines and instead added load 'deploy/assets to my Capfile. I don't think this makes any difference in the above problem but I wanted to document it anyway as adding your own recipe for pipeline precompiling is no longer necessary in Capistrano 2.8 as it was in the 3.1rc days.

Resources