Ruby on Rails 4 JavaScript Not Executed

Ruby on Rails 4 javascript not executed

$(document).ready(function() { } # Not working with turbo-links

From

Turbolinks overrides the normal page loading process, the event that
this relies on will not be fired. If you have code that looks like
this, you must change your code to do this instead:

$(document).on('ready page:load', function () {
// you code here
});

Another question

JavaScript not working in Ruby on Rails app

You can not access directly application.js or other js file functions from dom. Because rails use webpack for your codes. You can use events like this;

let openButton = document.querySelectorAll('button.openbtn')
openButton.addEventListener("click", function(event) {
document.getElementById("x").style.width = "15%";
})

let closeButton = document.querySelectorAll('button.closebtn')
closeButton.addEventListener("click", function(event) {
document.getElementById("mySidepanel").style.width = "0";
})

Rails 4 CoffeeScript not executing when precompiled

Finally found the problem, many thanks to @itsnikolay. The problem was due to the fact that another javascript file was throwing an exception. By having them all compiled into one single file, the exception would make the whole script stop running, thus not executing the Coffescripts.

In this particular case, it was due to the gem rails-assets-tether that was missing to provide tooltips from the bootstrap library.

When in debug mode, since, all javascript files are loaded independently, exceptions from one file doesn't affect loading the other ones.

Rails 4 - javascript files not loading in production but working well locally. (Heroku)

SOLVED:
In application.js I moved the file with the error to the bottom.
Everything seems to work fine.

Apparently in production mode files with warnings/error act differently.



Related Topics



Leave a reply



Submit