简体   繁体   中英

Rails 5 JavaScript asset pipeline issue

I'm trying to load some JavaScript in my rails application like so:

Application.html.erb (in the head)

<%= javascript_include_tag 'application' %>

Application.html.erb (before end of HTML)

<script>
  $(document).ready(function() {
    notie.alert(1, "It Works", 2);
  });
</script>

Application.js

//= require rails-ujs
//= require turbolinks
//= require theme/jquery
//= require theme/bootstrap
//= require theme/select2
//= require theme/notie
//= require theme/plugins
//= require theme/app
//= require_tree .

With this set up, I'm getting the following errors:

TypeError: document.body is null
TypeError: notie is undefined

It seems to me that the error may be because of turbolinks and how the JS is loaded with the asset pipeline but for the life of me I can't figure out where it's going wrong. Any help is appreciated.

The problem seems to be that your Javascript is calling for a <body> tag before it exists in the DOM. That's why you're getting the error:

TypeError: document.body is null

For Javascript like that it's best to load it at the bottom of the page instead of the Head.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM