Clinton J
Clinton J

Reputation: 2123

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

gem 'jquery-rails'

jQuery is loaded in application.js:

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

application.html.erb calls with the include tag:

<%= 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). 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:

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

This finally resulted in the ajax call within my jQuery script successfully calling a script. 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. 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.

Upvotes: 0

Views: 1099

Answers (2)

JohnPaul
JohnPaul

Reputation: 710

javascript file load issue use below code i think helped you

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

Switching back to

<%= javascript_include_tag "application" %>

Upvotes: 0

Pettrus Sherlock
Pettrus Sherlock

Reputation: 48

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

see if the jquery file is loading on your page.

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.

One last thing have you heard of bower? It is an awesome package manager, it is pretty easy to add depencies to your application.

Upvotes: 1

Related Questions