未捕获的ReferenceError:$未定义rails

时间:2022-11-07 08:56:27

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

问题只在生产服务器上(在localhost上工作正常)我正在使用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

4 个解决方案

#1


6  

Replace application.html.erb

替换application.html.erb

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

to

<%= javascript_include_tag "application" %>

#2


2  

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" %>

#3


1  

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();
});

#4


1  

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 %>

#1


6  

Replace application.html.erb

替换application.html.erb

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

to

<%= javascript_include_tag "application" %>

#2


2  

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" %>

#3


1  

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();
});

#4


1  

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 %>