2013-02-02 31 views
5

我使用Rails 3.2,它設置爲coffeescript。我知道Coffeescript是一種很棒的語言,學習起來並不難,但我只是開始圍繞Javascript和jQuery進行包裝。所以我的問題是這樣的:有沒有簡單的方法來設置rails 3.2以使用Javascript呢? 目前,我的jQuery是在我的視圖(timeline/index.html.erb)中的<script></script>標記中。我想將它移到.js文件中。我嘗試將timeline.js.coffee的名稱更改爲timeline.js並將jQuery放在那裏,但我獲得了Uncaught SyntaxError: Unexpected token ILLEGAL在rails 3.2中使用javascript而不使用coffeescript

除了「learn coffeescript」的明顯答案外,我該怎麼辦?

回答

5

我是在正確的軌道上拾起。要在rails 3.2中切換到javascript,只需要刪除.coffee擴展。但是,你還需要確保您使用//徵求意見,而不是#

//# Place all the behaviors and hooks related to the matching controller here. 
//# All this logic will automatically be available in application.js. 
//# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ 
0

清單文件和指令
http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives

加載JS在app /資產/ Java腳本文件,然後添加文件application.js


您的js文件

alert("here"); 

周的application.js

//= require_tree . 
or 
//= require your_js_file_name 

那麼,它就會從下面的代碼在你app/views/layouts/application.html.erb

<%= javascript_include_tag "application" %> 
+0

這一切都建立默認的方式。除了默認的filese是'.js.coffee',我試圖將其改爲'.js' –

0
$ rails -v 
Rails 3.2.11 
如何

這件事嗎?
http://bit.ly/VHEnBX

git clone it 
cd js_test 
bundle 
rails s -d 
open http://localhost:3000 

# stop the detached server 
kill -9 `cat tmp/pids/server.pid` 

你應該看到從瀏覽器here警報。

它只是一個簡單的js文件。沒有咖啡的延伸或任何。 http://bit.ly/UPe2mp

0

你可以簡單地註釋掉寶石「咖啡軌」在你的Gemfile

+0

它仍然應該將.js識別爲帶或不帶咖啡欄的JavaScript。它做什麼。 –