0

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

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

好吧,一些点击处理程序怎么样!所以资产/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,不太有用。这里发生了什么?如果我添加<script type="text/javascript" src='http://code.jquery.com/jquery-1.8.2.js'></script>到页面或我的 layout.html.erb,我们都很好。

尽管在 assets/javascripts 中,但下划线-min.js(注意它不是 .min.js)没有加载,我遇到了同样的问题。给定我的(未修改的)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 文件包含在其目录中

4

1 回答 1

4

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

于 2012-11-09T13:30:41.453 回答