0

I just deployed a rails app to Heroku and have found that there is a single javascript function. The function, officerCheck() lives in its own file officerCheck.js. And is called by an initialization file init.js with the line officerCheck.

The function is below; the first output to the log doesn't even show up

function officerCheck(){
    console.log("officer check in");
    var change=0;
    $('#toggle-two-wrapper').on('change', function(){
        if(change===0){
        checking();
        }else{
            change=0;
        }
    });
    function checking(){
        var alert=0;
        var existingRoles=[];
        neededRoles=["President", "Secretary", "Treasurer"];
        $("select.officer_role").each(function(i,e){
            existingRoles[i]=$(e).val();
            console.log("existing Roles "+existingRoles[i]);
        });
        $(neededRoles).each(function(i,e){
            if($.inArray(e,existingRoles)==-1){
                alert=1;
                $('div#final-submission').prepend("<span class='warning' style='display:block'>"+e+" needed.</span>");
                $("span.warning").delay(9000).slideUp("medium");
            }
        });
        console.log("alert equals "+alert);
        if(alert==1){
            console.log("we're in");
            change=1;
            $('div.incorporation_submit #toggle-two').bootstrapToggle('off');
        }
    }
}

Here is a list of what I've investigated so far:

  • All other javascript functions work (including those called by init.js)
  • The function officerCheck does indeed work on my localhost and heroku is up do date with that machine.
  • The function officerCheck and its respective call is indeed loaded by the browser when I'm browsing the heroku-hosted site. (I can see both in my script tab in firebug).
  • The contents of the function still work if I cut and paste them into firebug. (Leading me to believe that heroku just doesn't like how I'm calling the function)
  • Calling the function from firebug with officerCheck(); does NOT work.
  • However loading the function in its entirety into firebug and calling it with officerCheck(); DOES work.

I'm guessing that I'm pulling some sloppiness that Heroku just won't stand for. Any ideas thoughts or admonishments are welcome.

neanderslob
  • 2,633
  • 6
  • 40
  • 82
  • 2
    where is `change` declared? Maybe try another variable name and add the `var` declaration for starters. – WhiteHat Jul 23 '15 at 22:53
  • 2
    If you run in your console `officerCheck` without the parentheses, then you should get an output : `function`. If you don't, this means your function is not found and you should check your assets. Also, if you are running at localhost in development mode and in Heroku production mode, make sure you have compiled your assets AFTER having created the function. – Ruby Racer Jul 23 '15 at 22:53
  • @RubyRacer Good pro tip. The output from running `officerCheck` without parentheses was `officerCheck()` Does that count? I have indeed been running in development mode so I'll check on that order of operations. Thanks for the help. – neanderslob Jul 23 '15 at 23:07
  • @WhiteHat good catch. I forgot my vars. It didn't solve it but still, thanks for the QC. – neanderslob Jul 23 '15 at 23:16
  • Getting an existing function output, like you do, on a freshly loaded page, means that the function is indeed declared and loaded. You do have some issues with your variables not been declared, also in `checking()`, but that should be a browser thing and it should work or not regardless where your app is hosted.... Does your browser provide any feedback as to why the function is not ran? – Ruby Racer Jul 23 '15 at 23:38
  • @RubyRacer I I have no errors in my console so there's no indication on that front. I've edited to properly declared the variables (`change` and `existingRoles` and `alert`; will change in the above code) but it hasn't solved the situation. – neanderslob Jul 23 '15 at 23:54
  • @RubyRacer I gave the suggestion below a try and moved the function into `application.js` and it worked without a problem. What would that indicate that it was missing from the precompile? And if so, how could it have still shown up in firebug if it wasn't compiled? – neanderslob Jul 24 '15 at 00:09

1 Answers1

1

Rails apps running on Heroku may need a bit of work to get the asset pipeline to serve custom scripts.

You could move your script into application.js and check that it runs from there.

If you want to keep your script where it is, see Rails javascript asset missing after precompile

Community
  • 1
  • 1
David John Smith
  • 1,824
  • 2
  • 18
  • 22
  • Thanks for the tip. However, the script seems to be present as evidenced by it appearing in firebug's script tab and also by the troubleshooting that ruby racer has provided above in the comments. I don't believe that either would have worked if the asset was missing? – neanderslob Jul 23 '15 at 23:59
  • ...however, it DOES appear to work when I move the script into `application.js` like you suggested. I suppose that would indicate that it's missing from the asset pipeline. But how could it be "missing" but still appear in the application javascript file in firebug? – neanderslob Jul 24 '15 at 00:05