Application.js文件不在范围内

时间:2011-06-07 23:08:22

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

任何人都有这个问题 - 我无法从位于我的应用程序js文件中的Rails应用程序中的任何页面调用任何JS。我可以在外部文件中执行代码或嵌入在页面中但不是我的那个文件。我很难过。有任何想法吗。我正在尝试访问jqwery btw。

这是我的application.html.erb

    <%= javascript_include_tag "jquery.jeditable" %>
    <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" %>
    <%= javascript_include_tag "http://use.typekit.com/jdj1xqp.js" %>
    <%= javascript_include_tag :defaults %>

这是输出

    <script src="/javascripts/jquery.jeditable.js?1306352056" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <script src="http://use.typekit.com/jdj1xqp.js" type="text/javascript"></script>

    <script src="/javascripts/prototype.js?1305685037" type="text/javascript"></script>
    <script src="/javascripts/effects.js?1305685037" type="text/javascript"></script>
    <script src="/javascripts/dragdrop.js?1305685037" type="text/javascript"></script>
    <script src="/javascripts/controls.js?1305685037" type="text/javascript"></script>
    <script src="/javascripts/rails.js?1305685037" type="text/javascript"></script>
    <script src="/javascripts/application.js?1307485399" type="text/javascript"></script>

这是我的application.js

    // Place your application-specific JavaScript functions and classes here
    // This file is automatically included by javascript_include_tag :defaults
    $(document).ready(function(){

   $("#big-search-box").bind("keyup", function() {
         $("#big-search-box").addClass("loading"); // show the spinner
         var form = $("#live-search-form"); // grab the form wrapping the search bar.
         var url = "/schools/find"; // live_search action.  
         var formData = form.serialize(); // grab the data in the form  
         $.get(url, formData, function(html) { // perform an AJAX get
           $("#big-search-box").removeClass("loading"); // hide the spinner
           $("#live-search-results").html(html); // replace the "results" div with the         results
         });
      });
   });

1 个答案:

答案 0 :(得分:1)

简单问题,你正在加载jquery:

<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" %>

和原型:

<script src="/javascripts/prototype.js?1305685037" type="text/javascript"></script>

这些都会严重影响。您需要致电jQuery.noConflict

然后您需要更改任何jquery代码以使用jQuery.而不是$.

或者,您需要删除任何使用原型的东西,这是每个默认的rails js文件。

你的application.js应该是

jQuery(document).ready(function(){
    jQuery("#big-search-box").bind("keyup", function() {
        jQuery("#big-search-box").addClass("loading"); // show the spinner
        var form = jQuery("#live-search-form"); // grab the form wrapping the search bar.
        var url = "/schools/find"; // live_search action.  
        var formData = form.serialize(); // grab the data in the form  
        jQuery.get(url, formData, function(html) { // perform an AJAX get
            jQuery("#big-search-box").removeClass("loading"); // hide the spinner
            jQuery("#live-search-results").html(html); // replace the "results" div with the         results
        });
    });
});

有一个jQuery快捷方式可以对$进行重新别名,但这是一种更清晰的方法。