【问题标题】:$.getScript not working rails 3$.getScript 不工作 rails 3
【发布时间】:2012-02-07 20:08:45
【问题描述】:

除了$.getScript("/comments.js 部分之外,一切似乎都运行良好。我试图修复它,但没有运气。

我有我的application.js 文件,其中有:

$(function() {
setTimeout(test, 1000);

});

function test () {

$.getScript("/comments.js")
setTimeout(test, 1000);
}
$.getScript("/comments.js", function(){

 updateComments();
});

还有我在视图/cmets 中的 index.js.erb

function  updateComments(){
  alert("testing");
    }

我的 cmets 控制器

def index
# ...
end

布局/application.html.erb

  <%= stylesheet_link_tag  "application"  %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>

我只是想看看能不能让它工作,但它什么也没做。

【问题讨论】:

  • 你在使用资产管道吗?那样的话我认为是路线问题

标签: javascript jquery ruby ruby-on-rails-3


【解决方案1】:

如果您尝试使用您正在使用getScript 加载的脚本中的任何方法,请等到脚本加载完毕。使用它的回调方法,一旦脚本加载到页面上就会被调用。试试这个。

$.getScript("/comments.js", function(){
     //Write your code here
     updateComments();
});

还要确保js文件的路径正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    相关资源
    最近更新 更多