Ok, so my asset pipeline has suddenly started acting up on my development machine. JS functions that previously worked are now throwing “not a function” errors.. I know I must be doing something wrong. A minute ago the datatables jquery function was working, then it was throwing an error, then it was working, and now it’s not working or throwing an error.
Here is my application.js
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_self
//= require_tree .
//= require dataTables/jquery.dataTables
//= require dataTables/jquery.dataTables.bootstrap
//= require bootstrap
//= require bootstrap-tooltip
//= require bootstrap-popover
//= require bootstrap-tab
//= require bootstrap-modal
//= require bootstrap-alert
//= require bootstrap-dropdown
//= require jquery.ui.addresspicker
//= require raty
//= require jquery.alphanumeric
//= require jquery.formrestrict
//= require select2
//= require chosen/chosen.jquery
//= require highcharts
//= require jquery.lazyload
Here is some of my layout header:
<%= stylesheet_link_tag "application", media: "all" %>
<%= yield(:scripthead) %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= yield(:head) %>
Above I am using the yield to load up online scripts from google as they’re only needed on some pages, and generally slow down the site if included in the application layout. I tried removing the yield but things were still broken, even after clearing the browser cache and running rake assets:clean (just to be on the safe side).
Here’s what shows up between CSS and metatags (for a page with nothin in the yield scripthead):
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<script src="/assets/aidmodels.js?body=1" type="text/javascript"></script>
<script src="/assets/audio.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-alert.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-dropdown.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-modal.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-popover.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-tab.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-tooltip.js?body=1" type="text/javascript"></script>
<script src="/assets/branches.js?body=1" type="text/javascript"></script>
<script src="/assets/charts.js?body=1" type="text/javascript"></script>
<script src="/assets/chosen/backup_chosen.jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/chosen/chosen.jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/consumers.js?body=1" type="text/javascript"></script>
<script src="/assets/dispensers.js?body=1" type="text/javascript"></script>
<script src="/assets/favorites.js?body=1" type="text/javascript"></script>
<script src="/assets/features.js?body=1" type="text/javascript"></script>
<script src="/assets/generic_styles.js?body=1" type="text/javascript"></script>
<script src="/assets/gmaps4rails/gmaps4rails.base.js?body=1" type="text/javascript"></script>
<script src="/assets/gmaps4rails/gmaps4rails.bing.js?body=1" type="text/javascript"></script>
<script src="/assets/gmaps4rails/gmaps4rails.googlemaps.js?body=1" type="text/javascript"></script>
<script src="/assets/gmaps4rails/gmaps4rails.mapquest.js?body=1" type="text/javascript"></script>
<script src="/assets/gmaps4rails/gmaps4rails.openlayers.js?body=1" type="text/javascript"></script>
<script src="/assets/highcharts.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui-1.8.18.custom.min.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.alphanumeric.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.formrestrict.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.lazyload.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.ui.addresspicker.js?body=1" type="text/javascript"></script>
<script src="/assets/likes.js?body=1" type="text/javascript"></script>
<script src="/assets/messages.js?body=1" type="text/javascript"></script>
<script src="/assets/overalls.js?body=1" type="text/javascript"></script>
<script src="/assets/pages.js?body=1" type="text/javascript"></script>
<script src="/assets/questions.js?body=1" type="text/javascript"></script>
<script src="/assets/raty.js?body=1" type="text/javascript"></script>
<script src="/assets/reviews.js?body=1" type="text/javascript"></script>
<script src="/assets/sessions.js?body=1" type="text/javascript"></script>
<script src="/assets/styles.js?body=1" type="text/javascript"></script>
<script src="/assets/tickets.js?body=1" type="text/javascript"></script>
<script src="/assets/universities.js?body=1" type="text/javascript"></script>
<script src="/assets/users.js?body=1" type="text/javascript"></script>
<script src="/assets/dataTables/jquery.dataTables.js?body=1" type="text/javascript"></script>
<script src="/assets/dataTables/jquery.dataTables.bootstrap.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-transition.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-affix.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-button.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-carousel.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-collapse.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-scrollspy.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-typeahead.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap.js?body=1" type="text/javascript"></script>
<script src="/assets/select2.js?body=1" type="text/javascript"></script>
From application.rb:
config.assets.initialize_on_precompile = false
# Enable the asset pipeline
config.assets.enabled = true
config.action_controller.assets_dir = "#{File.dirname(File.dirname(__FILE__))}/public"
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
I’m sorry, I’m not sure what else to include to help with this puzzle, but any advise would be appreciated. I was having no problems before I started trying to upload to heroku and now everything’s gone haywire.
EDIT:
In the console at the moment I’m seeing
Uncaught TypeError: Cannot read property 'Constructor' of undefined bootstrap-popover.js:33
Uncaught ReferenceError: google is not defined jquery.ui.addresspicker.js:25
Uncaught TypeError: Object [object Object] has no method 'popover' overall:476
EDIT 2:
Thanks to one of the comments below I was able to figure this out. I had to re-shuffle the application.js includes to make more sense:
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require bootstrap
//= require bootstrap-tooltip
//= require bootstrap-popover
//= require bootstrap-tab
//= require bootstrap-modal
//= require bootstrap-alert
//= require bootstrap-dropdown
//= require dataTables/jquery.dataTables
//= require dataTables/jquery.dataTables.bootstrap
//= require jquery.ui.addresspicker
//= require raty
//= require jquery.alphanumeric
//= require jquery.formrestrict
//= require select2
//= require chosen/chosen.jquery
//= require highcharts
//= require jquery.lazyload
//= require_self
//= require_tree .
OMG, this is driving me bananas.. No, it still isn’t working. That last edit had everything working properly, but then I ran rake assets:precompile again and pushed to heroku.. It wasn’t working on Heroku again.. and believe it or not, when I got back to my localhost version it was broken again, and is now throwing up different errors and not loading datatables, again!
Uncaught ReferenceError: google is not defined jquery.ui.addresspicker.js:25
Uncaught ReferenceError: google is not defined application.js:26
Uncaught TypeError: Object [object Object] has no method 'lazyload' overall:496
Are you sure you didn’t forget a semicolon somewhere in the JS? The asset pipeline doesn’t do any code cleaning…
Take a look in your browser’s JS console to see where the errors are spitting up…
Edit: Ok, I think see what’s up. Try taking out the
require_treeat the end and manually load any other JS files you haven’t explicitly stated. What ends up happening is therequire_treeoverrides any previous requires that were explicitly listed and ends up loading them in alphabetical order.ALSO, I don’t know what gem you’re using for bootstrap.js (or if you even are using one), but also try using
twitter/bootstrapinstead of justbootstrap