新Rails 3.2.8 app不加载jQuery

时间:2012-11-08 13:54:08

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

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

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

好吧,点击处理程序怎么样!所以assets / javascripts / searching.js.coffee:

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

而且......没什么。所以我转到Chrome控制台:

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

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

我遇到同样的问题,下划线-min.js(注意它不是.min.js)尽管在资源/ 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文件包含在其目录

1 个答案:

答案 0 :(得分:4)

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