簡體   English   中英

在rails 3.2中使用javascript而不使用coffeescript

[英]use javascript in rails 3.2 without coffeescript

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

除了“學習coffeescript”的明顯答案外,我該怎么辦?

我走在正確的軌道上。 要在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/

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

在app / assets / javascripts中添加js文件,然后在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" %>
$ 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文件。 沒有.coffee擴展或任何。 http://bit.ly/UPe2mp

你可以簡單地在你的Gemfile中注釋掉gem'coffee-rails'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM