5

我正在使用 Rails 3.2,它是为咖啡脚本设置的。我知道 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”的明显答案,我该怎么办?

4

4 回答 4

6

我走在正确的轨道上。要在 rails 3.2 中切换到 javascript,您只需删除.coffee扩展。但是,您还需要确保使用//for comments 而不是#

//# 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/
于 2013-02-02T17:38:08.977 回答
1

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

在 app/assets/javascripts 中添加 js 文件,然后将文件添加到application.js


你的js文件

alert("here");

应用程序.js

//= require_tree .
or
//= require your_js_file_name

然后,它将从您下面的代码中获取app/views/layouts/application.html.erb

<%= javascript_include_tag "application" %>
于 2013-02-02T05:28:09.380 回答
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文件。没有 .coffee 扩展名或任何扩展名。 http://bit.ly/UPe2mp

于 2013-02-03T12:05:37.273 回答
0

您可以在 Gemfile 中简单地注释掉 gem 'coffee-rails'

于 2013-11-05T21:41:09.590 回答