2012-11-08 31 views
0

我做了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文件

+1

你有'javascript_include_tag「應用程序」somwehere在你的佈局?你可以檢查文件實際上是在頁面的html中調用嗎? –

+0

你的gemfile中是否有gem'jquery-rails',並且你是否安裝了bundle? –

+0

@AnthonyAlberto默認Rails應用負載'<%= javascript_include_tag 「應用程序」 %>' @AndreasLyngstad沒錯'寶石「的jquery-rails''處於的Gemfile ' – Chris

回答

4

鏈輪不是出於某種原因的工作。檢查你是否正確地包含了所有與資產相關的gem,並且你沒有更改config/environments/development.rb。顯然,確保public/javascripts /中沒有application.js,它應該在app/assets/javascripts中。

+1

你是約鏈輪正確。我在Ruby 2.0上,需要更高版本的鏈接,如https://github.com/rails/rails/issues/7531搖擺和滾動! – Chris

+0

嘿嘿,你沒有發現提及你使用Ruby 2.0的重要性嗎? :-)我很高興你有它的工作! – mrbrdo

相關問題