Uncaught ReferenceError: $ is not defined rails

2019-02-21 13:15发布

I'm trying to add Froala editor to my project.

Problem only on production server(on localhost it works fine) I'm using rails 4.1.0 In gemfile i'm have

gem 'jquery-rails'

In my 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:

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

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

5条回答
Fickle 薄情
2楼-- · 2019-02-21 13:32

An solution working for me is:

Separate files

create assets/javascripts/jquery_init.js content :

//= require jquery
//= require jquery.turbolinks
//= require jquery.cookie
//= require jquery.ui.all
//= require jquery_ujs

an call before aplication.js

<%= javascript_include_tag "jquery_init" %>
<%= javascript_include_tag "application", :async => !Rails.env.development?, "turbolinks-data-track" => true %>
查看更多
贼婆χ
3楼-- · 2019-02-21 13:50

How do you have the foundation added to the project?

In app/assets/javascripts/aplication.js

I had the same error

$ is no defined

and I resolved by reloading the foundation js this way:

jQuery(document).ready(function($) {
$(document).foundation();
});
查看更多
淡お忘
4楼-- · 2019-02-21 13:50

This was my solution:

Put this in your app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs

and install this gem file:

gem 'jquery-rails'
查看更多
时光不老,我们不散
5楼-- · 2019-02-21 13:54

Replace application.html.erb

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

to

<%= javascript_include_tag "application" %>
查看更多
神经病院院长
6楼-- · 2019-02-21 13:54

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" %>
查看更多
登录 后发表回答