title: Ruby Rails - Javascript with AJAX POST working locally but not Heroku
body: <p>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. </p>
  
  <p>I have the following in <code>application.js</code>:</p>
  
  <pre><code>//= 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
        });
  }
  </code></pre>
  
  <p>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 <code>onload</code> and <code>onbeforeunload</code> 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?</p>
  
  <p>There are a lot of answers out there about precompiling assets and then pushing to Heroku, or adjusting various options in the <code>production.rb</code> or <code>application.rb</code> file, have tried a bunch of those to no avail. Also some answers report ordering of the <code>//=...</code> lines in the <code>application.js</code> file matters (having something to do with bootstrap), I've tried that too but again nothing.</p>
  
  <p>It must have something to do with the variables I am declaring in the event functions, or the <code>async:none</code> 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.</p>
  
  <p>Update 1:</p>
  
  <p>I found the following: <a href="https://stackoverflow.com/questions/19237938/javascript-not-loading-on-heroku-but-works-locally">JavaScript not loading on Heroku but works locally</a> which sounds very similar to my problem. Could this be it?</p>
  
  <p>Update 2:</p>
  
  <p>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 <code>onbeforeunload</code> 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?</p>