【问题标题】:New Rails 3.2.8 app not loading jQuery新的 Rails 3.2.8 应用程序未加载 jQuery
【发布时间】:2012-11-08 13:54:08
【问题描述】:

我今天做了一个rails new,然后创建了一个简单的搜索栏:

<form>
  <input id='search-bar' type='search' name='search'></input>
</form>
<a id='search-btn' href="#">searching!</a>

好的,一些点击处理程序怎么样!所以 assets/javascripts/searches.js.coffee:

$('#search-btn').click ->
  alert 'BOOM'
  query = $('#search-bar').html
  console.log query

而且……什么都没有。所以我去 Chrome 控制台:

>$('a')
null
>$('body')
null
>$
function () { [native code] }

一个没有选择器的 jQuery,没有太大帮助。这里发生了什么?如果我将&lt;script type="text/javascript" src='http://code.jquery.com/jquery-1.8.2.js'&gt;&lt;/script&gt; 添加到页面或我的layout.html.erb,我们都很好。

我遇到了与 underscore-min.js 相同的问题(注意它不是 .min.js),尽管位于 assets/javascripts 中,但无法加载。鉴于我的(未修改的)application.js,它应该加载当前文件夹:

//= require jquery
//= require jquery_ujs
//= require_tree .

更新

如果我转到 Chrome 检查器的资源选项卡并检查 application.js,我会看到:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
;

这看起来不像是在其目录中正确包含 js 文件

【问题讨论】:

  • 你的布局中有javascript_include_tag "application" somwehere吗?你能检查一下这些文件实际上是在页面的 html 中调用的吗?
  • 你的 gemfile 中有 gem 'jquery-rails' 并且你做了捆绑安装吗?
  • @AnthonyAlberto 默认 Rails 应用程序加载 &lt;%= javascript_include_tag "application" %&gt; @AndreasLyngstad 是的 gem 'jquery-rails' 在 Gemfile 中 `

标签: jquery ruby-on-rails asset-pipeline


【解决方案1】:

链轮由于某种原因无法工作。检查您是否正确包含所有与资产相关的 gem,并且您没有更改 config/environments/development.rb。显然要确保 public/javascripts/ 中没有 application.js,它应该在 app/assets/javascripts 中。

【讨论】:

  • 您对 Sprockets 的看法是正确的。我使用的是 Ruby 2.0,这需要更高版本的链轮,如此处提到的 github.com/rails/rails/issues/7531 摇摆不定!
  • 嘿嘿,你不觉得提到你在 Ruby 2.0 上很重要吗? :-) 我很高兴你能成功!
猜你喜欢
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多