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.

7
  • 2
    where is change declared? Maybe try another variable name and add the var declaration for starters. Commented Jul 23, 2015 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. Commented Jul 23, 2015 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. Commented Jul 23, 2015 at 23:07
  • @WhiteHat good catch. I forgot my vars. It didn't solve it but still, thanks for the QC. Commented Jul 23, 2015 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? Commented Jul 23, 2015 at 23:38

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
...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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.