简体   繁体   中英

Javascript doesn't work unless I include it directly in the view

So I'm trying to utilize a few jquery plugins in my ruby on rails project. However, none of them work unless I import them via a cdn or put the code directly into the view. I'm not sure why this is occurring.

This is my application.js

//= require bootstrap-sprockets
//= require bootstrap
//= require bootstrap.js
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require jquery.bootstrap-autohidingnavbar.js
//= require jquery.scrolline.js

I tried using just 'require_tree ." but that didn't work. So, I tried to include specifically the plugins that I needed. That still didn't work. The only thing that works is when the code is written directly in my view file, and then I call it.

So I guess what the issue is, you have added jquery to your applicatoin.html.erb from cdn. And You have done it after you have done your javascript_include_tag which includes the manifest file.

You have to include javascript first and then your application.js file. And if you if you're doing it via cdn, you can get rid of //= require jquery from your application.js file.

Let me know if it solves the issue.

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