我刚刚将一个 Rails 应用程序部署到 Heroku,发现只有一个 javascript 函数。函数 officerCheck()
位于其自己的文件 officerCheck.js
中。并由初始化文件 init.js
使用 officerCheck
行调用。
函数如下;日志的第一个输出甚至没有显示
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');
}
}
}
以下是我迄今为止调查过的内容的列表:
- 所有其他 JavaScript 函数都可以工作(包括由 init.js 调用的函数)
- 函数
officerCheck
确实可以在我的本地主机上运行,并且 heroku 已与该机器保持同步。 - 当我浏览 heroku 托管的网站时,函数
officerCheck
及其各自的调用确实由浏览器加载。 (我可以在 firebug 的脚本选项卡中看到两者)。 - 如果我将函数的内容剪切并粘贴到 Firebug 中,它们仍然有效。 (让我相信 Heroku 只是不喜欢我调用该函数的方式)
- 使用
officerCheck();
从 firebug 调用该函数不起作用。 - 但是,将函数整个加载到 firebug 中并使用
officerCheck();
调用它确实有效。
我猜我有些草率,Heroku 无法容忍。欢迎任何想法或建议。
最佳答案
在 Heroku 上运行的 Rails 应用程序可能需要一些工作才能让 Assets 管道为自定义脚本提供服务。
您可以将脚本移至 application.js 并检查它是否从那里运行。
如果您想将脚本保留在原处,请参阅 Rails javascript asset missing after precompile
关于javascript - 单个 Javascript 函数无法在 Heroku 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31599266/