将外部JS链接到rails应用程序

时间:2011-08-05 13:18:49

标签: javascript jquery ruby hyperlink

这应该是一个简单的但我无法正确理解语法。

我也找不到任何外部链接文件的例子,我发现的所有搜索结果似乎都是通过存储文件的本地副本来解决他们的问题。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>

这是我的.erb文件中的一个简单的外部脚本标记,但rails没有拾取任何不使用

的内容
<%= javascript_include_tag 'whatever.js' %>

如何将JS外部链接到我的rails应用程序。

由于技术要求,我无法在本地存储Jquery.js(我也不理解)


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

也许问题不在于我没有正确引用文件。它似乎是在html.erb文件中生成的,但是在firebug控制台中,我得到的Jquery没有定义。

jQuery is not defined
[Break On This Error] remText=remText.replace(/\%n/g,charsRe...al:true,allowExceed:false};})(jQuery);
jquery...9702940 (line 17)
jQuery is not defined
[Break On This Error] })(jQuery); 
protot...0469242 (line 5653)

生成的HTML链接。 我不知道为什么标准的src =标签不会找到它。

     <link href="/stylesheets/globalStyleSheet.css?1312383253" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/jquery.inputlimiter.1.0.css?1312464674" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/style.css?1312469715" media="screen" rel="stylesheet" type="text/css" />
      <script src="/javascripts/prototype.js?1310469242" type="text/javascript"></script>
    <script src="/javascripts/effects.js?1310469242" type="text/javascript"></script>
    <script src="/javascripts/dragdrop.js?1310469242" type="text/javascript"></script>

    <script src="/javascripts/controls.js?1310469242" type="text/javascript"></script>
    <script src="/javascripts/rails.js?1310469242" type="text/javascript"></script>
    <script src="/javascripts/application.js?1310469242" type="text/javascript"></script>


    <link href="/stylesheets/globalStyleSheet.css?1312383253" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/style.css?1312469715" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/jquery.inputlimiter.1.0.css?1312464674" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/globalStyleSheet.css?1312383253" media="screen" rel="stylesheet" type="text/css" />
<link href="/stylesheets/jquery.inputlimiter.1.0.css?1312464674" media="screen" rel="stylesheet" type="text/css" />
<link href="/stylesheets/style.css?1312469715" media="screen" rel="stylesheet" type="text/css" />

    <script src="/javascripts/jquery.inputlimiter.1.2.2.min.js?1299702940" type="text/javascript"></script>
    <script src="/javascripts/globalviewFunctions.js?1312381897" type="text/javascript"></script>

    <script src="/javascripts/editInPlace.js?1312468960" type="text/javascript"></script>
    <script src="/javascripts/errorDisplay.js?1312381897" type="text/javascript"></script>
    <script src="/javascripts/journeyViewFunctions.js?1312541017" type="text/javascript"></script>
    <script src="/javascripts/pageviewFunctions.js?1312449379" type="text/javascript"></script>
    <script src="/javascripts/shaneShowHide.js?1312387899" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>

    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
    <script src="/javascripts/prototype.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/effects.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/controls.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/rails.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/application.js?1310469242" type="text/javascript"></script>
    <script src="/javascripts/prototype.js?1310469242" type="text/javascript"></script>

<script src="/javascripts/effects.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/controls.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/rails.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/Legacy.js?1312381897" type="text/javascript"></script>
<script src="/javascripts/editInPlace.js?1312468960" type="text/javascript"></script>
<script src="/javascripts/errorDisplay.js?1312381897" type="text/javascript"></script>
<script src="/javascripts/globalviewFunctions.js?1312381897" type="text/javascript"></script>
<script src="/javascripts/journeyViewFunctions.js?1312541017" type="text/javascript"></script>

<script src="/javascripts/jquery.inputlimiter.1.2.2.min.js?1299702940" type="text/javascript"></script>
<script src="/javascripts/jquery.min.js?1312544046" type="text/javascript"></script>
<script src="/javascripts/pageviewFunctions.js?1312449379" type="text/javascript"></script>
<script src="/javascripts/shaneShowHide.js?1312387899" type="text/javascript"></script>
<script src="/javascripts/application.js?1310469242" type="text/javascript"></script>

1 个答案:

答案 0 :(得分:2)

在我的一个应用程序中,我还使用javascript_include_tag来包含javascripts。唯一不同的是我给它一个数组(它被动态填充)。它有点像这样:

def add_javascript(url)
  @javascript_urls ||= []
  @javascript_urls << url unless @javascript_urls.include?(url)
end

def create_javascript_urls()
  javascript_include_tag(@javascript_urls) + "\n" unless @javascript_urls.blank?
end

添加我使用的jQuery javascript:

add_javascript('http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js')

与您的问题示例相比,这似乎与我相似,但也许它可以帮助您找到答案......