简体   繁体   English

jQuery不在Rails服务器上加载-'$未定义'

[英]jquery not loading on rails server - '$ is not defined'

I have a rails app that is failing to load jQuery when called in some embedded JS within one of my views. 我有一个Rails应用程序,当在我的其中一个视图中的某些嵌入式JS中调用时,无法加载jQuery。 I've looked around and seen some common solutions, so I'll list what I've already done: 我环顾四周,看到了一些常见的解决方案,因此我将列出已经完成的工作:

jQuery is listed uncommented in my Gemfile: jQuery在我的Gemfile中未加注释:

gem 'jquery-rails'

jQuery is loaded in application.js: jQuery已加载到application.js中:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

application.html.erb calls with the include tag: 带有include标记的application.html.erb调用:

<%= javascript_include_tag 'application', 'data-turbolinks-track'=> true %>

I've also tried: 我也尝试过:

<%= javascript_include_tag 'application' %>

Where could this be going wrong? 这可能在哪里出错?

EDIT: 编辑:

I was able to get it working but it is a definite workaround and I'd like to figure out a more permanent solution. 我能够使其正常运行,但这是一个确定的解决方法,我想找出一个更永久的解决方案。 Firstly I found that a coworker had set this server up a little differently and application.html.erb was being overridden by another layout, which did not contain the javascript_include_tag line in the head (or anywhere else in the file). 首先,我发现一个同事对该服务器的设置有所不同,并且application.html.erb被另一种布局覆盖,该布局的头部(或文件中的其他任何地方)不包含javascript_include_tag行。 Despite adding that line of code in, the error persisted. 尽管添加了该行代码,该错误仍然存​​在。 I saved jquery.min.js from the online API link ( https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js ) and placed it in the assets folder, then precompiled and added the following line to the head of the layout: 我从在线API链接( https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js )中保存了jquery.min.js并将其放置在资产文件夹中,然后进行了预编译和将以下行添加到布局的开头:

<%= javascript_include_tag 'jquery.min', 'data-turbolinks-track'=> true %>

This finally resulted in the ajax call within my jQuery script successfully calling a script. 最终,这导致我的jQuery脚本中的ajax调用成功调用了脚本。 However, there are still two problems: first, as I said above, I'd like a more permanent and less hackish solution, and second, the success function from the ajax call is not working, despite it working correctly on my private testing server. 但是,仍然存在两个问题:首先,如上所述,我想要一个更持久,更可靠的解决方案,其次,尽管ajax调用的成功功能在我的专用测试服务器上正常运行,但它无法正常运行。 The code is the same between the two, so I know it is not a problem in the code, and I am almost certain it is connected to whatever the underlying problem that is messing with jQuery in general. 两者之间的代码是相同的,所以我知道这不是代码中的问题,而且我几乎可以肯定,它与通常困扰jQuery的任何潜在问题有关。

Try adding: <%= javascript_include_tag 'application','jquery', 'data-turbolinks-track' => true %> 尝试添加:<%= javascript_include_tag'application','jquery','data-turbolinks-track' => true %>

see if the jquery file is loading on your page. 查看是否在页面上加载了jquery文件。

Also, the workaround you did is not hacky or bad by any means, actually for me i prefer to let gulp take care of all my assets, the way ruby takes care of your assets is pretty slow and clunky, especially if you are trying to integrate something like angular with ui router, the same assets that i compile and mangle on gulp takes about 10s, on ruby takes more than 1 minute. 另外,您所做的变通办法无论如何都不算​​顽强或糟糕,实际上对我而言,我更喜欢让gulp照顾我的所有资产,ruby照顾您的资产的方式相当缓慢且笨拙,尤其是在您尝试将类似angular的东西与ui路由器集成在一起,我在gulp上编译和处理的​​相同资产大约需要10秒钟,在ruby上则需要1分钟以上。

One last thing have you heard of bower ? 您听说过凉亭的最后一件事吗? It is an awesome package manager, it is pretty easy to add depencies to your application. 这是一个很棒的软件包管理器,很容易将依赖关系添加到您的应用程序中。

javascript file load issue use below code i think helped you javascript文件加载问题使用下面的代码,我认为对您有帮助

<%= javascript_include_tag "application", data-turbolinks-track'=> true %>

Switching back to 切换回

<%= javascript_include_tag "application" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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