jQuery 未捕获的 ReferenceError: $ is not defined rails
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29327004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught ReferenceError: $ is not defined rails
提问by bartezr
I'm trying to add Froala editor to my project.
我正在尝试将 Froala 编辑器添加到我的项目中。
Problem only on production server(on localhost it works fine) I'm using rails 4.1.0 In gemfile i'm have
仅在生产服务器上出现问题(在本地主机上它工作正常)我正在使用 rails 4.1.0 在 gemfile 中我有
gem 'jquery-rails'
In my assets/javascripts/application.js:
在我的 assets/javascripts/application.js 中:
//= require jquery
//= require jquery_ujs
//= require foundation
//= require turbolinks
//= require_tree .
//= require modernizr
//= require froala_editor.min.js
In new.html.erb file:
在 new.html.erb 文件中:
<div class="row">
<%= form_for :article do |f| %>
<p>
Title<br>
<%= f.text_field :title %>
</p>
<p>
Content<br>
<%= f.text_area :text, :id=>'content' %>
<p>
<%= f.submit %>
</p>
<% end %>
</div>
<script>
$(function() {
$('div#content').editable({
inlineMode: false
})
});
</script>
In application.html.erb:
在 application.html.erb 中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><%= content_for?(:title) ? yield(:title) : "foundation-rails" %></title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag "vendor/modernizr" %>
<%= javascript_include_tag "application" 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
In this case result is:
在这种情况下,结果是:
Uncaught ReferenceError: $ is not defined
If i'm adding a string:
如果我要添加一个字符串:
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
result is:
结果是:
Uncaught TypeError: undefined is not a function
回答by jasmineq
This was my solution:
这是我的解决方案:
Put this in your app/assets/javascripts/application.js
把它放在你的 app/assets/javascripts/application.js 中
//= require jquery
//= require jquery_ujs
and install this gem file:
并安装这个 gem 文件:
gem 'jquery-rails'
回答by Vedprakash Singh
Replace application.html.erb
替换 application.html.erb
<%= javascript_include_tag "application" 'data-turbolinks-track' => true %>
to
到
<%= javascript_include_tag "application" %>
回答by Riru Isla
How do you have the foundation added to the project?
您如何将基础添加到项目中?
In app/assets/javascripts/aplication.js
在 app/assets/javascripts/aplication.js
I had the same error
我有同样的错误
$ is no defined
$ 没有定义
and I resolved by reloading the foundation js this way:
我通过这种方式重新加载基础js来解决:
jQuery(document).ready(function($) {
$(document).foundation();
});
回答by cristi_razvi
try switching these lines:
尝试切换这些行:
<%= javascript_include_tag "vendor/modernizr" %>
<%= javascript_include_tag "application" 'data-turbolinks-track' => true %>
will become
会变成
<%= javascript_include_tag "application" 'data-turbolinks-track' => true %>
<%= javascript_include_tag "vendor/modernizr" %>
回答by scampetel
An solution working for me is:
对我有用的解决方案是:
Separate files
单独的文件
create assets/javascripts/jquery_init.js content :
创建 assets/javascripts/jquery_init.js 内容:
//= require jquery
//= require jquery.turbolinks
//= require jquery.cookie
//= require jquery.ui.all
//= require jquery_ujs
an call before aplication.js
aplication.js 之前的调用
<%= javascript_include_tag "jquery_init" %>
<%= javascript_include_tag "application", :async => !Rails.env.development?, "turbolinks-data-track" => true %>