-
Notifications
You must be signed in to change notification settings - Fork 384
Trouble shooting: Jquery issues
Please note: This is written for beginner programmers ... see Common Issues & Errors to get a list of issues in the short form.
Jquery scripts can be located almost anywhere in Rails projects, mostly it's a choice of performance or special circumstances. The Rails way is to use the JavaScript helper, which is located in the application file, in order to load the asset pipeline, including all files in the JavaScript directory.
Specifically, ...
- Rails hits the
javascript_include_tag 'application'
line in the/app/views/layout/application.html.erb
- Then looks for the
application.js
in theapp/assets/javascript/appliction.js
- Finally, the
application.js
loads all the JavaScript files located in theapp/assets/javascript
directory via the//= require_tree .
entry at the bottom of the file.
Asset Pipeline is described offically here ... Rails Guides: Asset Pipeline
- Check your application on a different browser
- Check your application on a different computer
- Check if the issue is a loading issue
- Check if the issue is a possible Jquery library conflict
- Check if it's a syntax error
- Check if the previous jquery is hanging
First we need to decide if it's a loading issue...
- When testing by hand, did you see any Jquery effects firing?
- Open the Chrome web browser console
- Run the page in question
- Open the Chrome dev tools by ...
- On the page near your fields, Right click & select
Inspect
- On the page near your fields, Right click & select
- Check the
elements
section in the upper right for your javascript file names. You should see the Cocoon was loaded under theelements
section. - If you wrote custom callbacks - then check next to the
element
, for thecallback
section to see if those registered in the web console.
Please note, just because you are seeing things in the elements
, it does not necessarily mean the Jquery code you wrote is firing!
If any of the above are absent you probably will want to check your setup or files for the Jquery setup in rails
- Open the gemfile, ensure your Jquery library & cocoon are listed (run a
bundle install
if needed) - Double check the gemfile.lock has the Jquery library & cocoon (run a
bundle update
if needed) - Open the
/layouts/application.html.erb
file & ensured there was only onejavascript_include_tag
. - Also that the
javascript_include_tag
was in the<head>
tag & that the head tag is properly closed with</head>
- Look for other non-essential or extra service loading files
- Check the
/app/assets/javascript
folder for theapplication.js
file - Check that there's no
.coffee
files (rails autos them if certain gems are installed) - Check inside the
application.js
file ...- Check order of your
/app/assets/javascript/application.js
files being loaded to ensure//= require_tree .
at bottom of list. It absolutely must be at the bottom or you're going to have issues including all the files manually into theapplication.js
- Additionally, all files above must have the required,
//=
in front of them
- Check order of your
- Check the
If the asset is loading in the Chrome web browser elements
section, we know it's either a conflict or a syntax issue
- Insert a link in the in the
app/assets/javascript/application.js
to a file. - Make a file to hold my test code. I name my file
app/assets/javascript/test.js
. Make sure rails doesn't rename the file after you save it, as certain parts of rails play with the assets directory. - The file should log / output to console.
- In it you need this code snippet if not using turbolinks ...
$(document).on("ready", function() {
console.log("-- Page Load --");
});
- If using turbolinks ...the first line is different
$(document).on('turbolinks:load', function() {
console.log("-- Page Load --");
});
Congratulations - you installed the Jquery right & there's no conflicts. It is just your code syntax that needs to be corrected.
It's likely a conflict with your Jquery or javascript somewhere in the project. To test this ...
- Try using stopPropagation. It should go near the top of the function definition that needs to run.
- Check the gemfile for having multiple versions of Jquery libraries & comment out the duplicates
- At this point, if I'm doing raw JavaScript or Jquery files, I will comment out
gem coffee-rails
in my gemfile (be sure tobundle update
) - If not sure they are duplicating read the github repository & search for closed issues between the two gems you are using that both utilize Jquery.
- At this point, if I'm doing raw JavaScript or Jquery files, I will comment out
- Ensure no extra scripts being loaded in the views, I use
grep -r 'script' /app
& check any spot that it finds. Just comment the view's<script>
(along with any code here)</script>
out or store them in a blank file as the.erb
format does not comment out very well sometimes. - Pull all the entries from the
app/assets/javascript/application.js
and put them in a blank file to add back in to theapplication.js
later. Below is the bare minimum of files I use in the in theapplication.js
.
//= require jquery_ujs
//= require cocoon
//= require turbolinks
//= require test.js
- In the
app/views/layouts/application.html.erb
- Use
javascript_include_tag
to include a CDN version of Jquery library.- Latest Jquery CDN here:
https://code.jquery.com/
)
- Latest Jquery CDN here:
- How to use it here:
https://jquery.com/download/#using-jquery-with-a-cdn
- Use
Now we put the application back together to locate the conflicting files.
- In your
app/assets/javascript/application.js
, at the bottom of the file up the//= .
& save it ... - You should re-add your js files to the app/assets/javascript one at a time, testing each time by refreshing the webpage, possibly restarting the server after copying the file in to the
application.js
If you are not using a CDN you will need to be doing a rake or rails assets like shown below based on version - For versions before Rails 5, use
rake assets:clean
& therake assets:precompile
- For Rails 3 & 4 use
rails assets:clean
& therails assets:precompile
... I believe Rails 1 & 2 didn't use asset pipeline, but someone made a Rails 2 gem for backwards compatibility ...
The Rails 2 gem is here ... Rails 2 Asset Pipeline