I seem to be experiencing some sort of perennial problem regarding JS and Heroku. Although not exactly---some of my JS works, but not all.
I have the following in application.js
:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
function submitForm(pagenamed, starttime, endtime) {
alert("checked the button - worked");
$.ajax({
type:'POST',
url: '/pagetimes/create',
data: $.param({ pagetime: {pagename: pagenamed, start: starttime, end: endtime }}),
async:false
});
}
var d = new Date();
var starttime = d.getTime();
var pagename = document.URL
alert("load works START:" +d+ starttime + pagename);
window.onbeforeunload=function() {
var j = new Date();
var endtime = j.getTime();
alert("unload works START:" +d+ starttime +"END:"+j+ endtime +pagename );
$.ajax({
type:'POST',
url: '/pagetimes/create',
data: $.param({ pagetime: {pagename: pagename, start: starttime, end: endtime }}),
async:false
});
}
Which, as you may guess, is a JS to capture time spent on a page and record it under a model (pagetimes) via sending an AJAX call to the create action (all without user input). I am still developing so I have left a bunch of alerts in to show me when the functions get fired. There is also a function which I can call using a button which effectively does the same thing (how I debugged the AJAX call when I built it). All of this works seamlessly in localhost (development), but only the button works in production on Heroku. And even given that, both the onload
and onbeforeunload
actions fire on Heroku when expected (I get the alerts, exact same as in development), but nothing shows up in my database. Any thoughts on whats going wrong here?
There are a lot of answers out there about precompiling assets and then pushing to Heroku, or adjusting various options in the production.rb
or application.rb
file, have tried a bunch of those to no avail. Also some answers report ordering of the //=...
lines in the application.js
file matters (having something to do with bootstrap), I've tried that too but again nothing.
It must have something to do with the variables I am declaring in the event functions, or the async:none
option? Otherwise I'm out of ideas. Am using the same browser for both localhost and heroku app. Is there anything obvious I might be missing? Using Rails 3, Win7, no fancy gems.
Update 1:
I found the following: JavaScript not loading on Heroku but works locally which sounds very similar to my problem. Could this be it?
Update 2:
I included the pagename variable in my alert to see if that wasn't working properly. Not the problem, the correct info appears in the alert. Also made the button and onbeforeunload
async options the same in case that was it. Is not. Now everything about these ajax calls is the exact same, but one works and the other doesn't. What gives?