我做了rails new
今天,然後創建一個簡單的搜索欄:新的Rails 3.2.8應用程序不加載的jQuery
<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,我們都很好。
我與underscore-min.js(注意它不是.min.js)沒有加載,儘管是在assets/javascripts中有同樣的問題。鑑於我的(未修改)的application.js,它應該加載當前文件夾:如果我去了Chrome檢查的資源選項卡,並檢查了的application.js
//= require jquery
//= require jquery_ujs
//= require_tree .
UPDATE
,我看到:
// 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「應用程序」somwehere在你的佈局?你可以檢查文件實際上是在頁面的html中調用嗎? –
你的gemfile中是否有gem'jquery-rails',並且你是否安裝了bundle? –
@AnthonyAlberto默認Rails應用負載'<%= javascript_include_tag 「應用程序」 %>' @AndreasLyngstad沒錯'寶石「的jquery-rails''處於的Gemfile ' – Chris