0

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.

7
  • Did you Restart the server after bundle install ? Commented Dec 21, 2016 at 2:38
  • Yes, I did. Several times Commented Dec 21, 2016 at 2:40
  • If you inspect that page source do you see jquery getting loaded in in a script tag? Commented Dec 21, 2016 at 2:45
  • No, I do not see that. Commented Dec 21, 2016 at 2:47
  • Just to be clear it will be cached so it will look something like this: "<script src="/assets/jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1" data-turbolinks-track="reload"></script>" It *should be in a huge check of these at the top of the page. Commented Dec 21, 2016 at 2:51

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I am having trouble understanding what you are trying to communicate, can you please edit your comment for clarification and/or proofreading?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.