如何在 Rails 3.1 应用程序中完全禁用 CoffeeScript?
当我生成一个新的控制器时,Rails 也会为该控制器生成一个 .js.coffee 文件。因为我不使用 CoffeeScript,所以我希望 Rails 为我生成 .js
文件。
注释掉 coffee-rails
gem 是否足以在 Rails 3.1 应用程序中完全禁用 CofeeScript?
At the moment when I generate a new controller, Rails also generates a .js.coffee
file for the controller as well. As I don't use CoffeeScript I want Rails instead generate .js
files for me.
Is it enough to comment out the coffee-rails
gem to completely disable CofeeScript in a Rails 3.1 app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
gem "coffee-script"
gem "coffee-script"
in your Gemfile不确定这对于 Rails 3.1 是否有效,但在 4 中,您还应该将
application.rb
中的javascript_engine
设置为:js
以指示生成器创建.js
文件而不是.js.coffee
。Not sure if this counts for Rails 3.1 but in 4 you should also set the
javascript_engine
to:js
inapplication.rb
to instruct generators to create.js
files instead of.js.coffee
.Koen 和 Gaurav Gupta 给出了很好的答案!
如果您想对每个新的 Rails 项目自动进行这些更改,您可以使用模板文件。
在
~/rails-template.rb
然后在
~/.railsrc
现在每当你运行
rails new
时,coffeescript gem 都会被注释掉,新控制器将使用.js
而不是.coffee
。在 Rails 5.0.4 上进行了测试,但我相信它也应该适用于早期版本。
顺便说一句,Rails 模板 和 一般来说,生成器非常强大。我是一名教师,我的学生通常会在课程中创建 15 到 20 个 Rails 项目,为他们提供一个良好的模板文件,其中包含调试 gem、规范样式测试等,可以节省大量时间。当然,在他们自己做出改变之后。如果您有兴趣,我个人的
.rails-template.rb
是 在 GitHub 上。Koen and Gaurav Gupta have good answers!
If you want to make these changes automatically for every new Rails project, you can use a template file.
In
~/rails-template.rb
Then in
~/.railsrc
Now whenever you run
rails new
, the coffeescript gem will be commented out, and new controllers will use.js
instead of.coffee
.Tested on Rails 5.0.4, but I believe it should work for earlier versions as well.
As an aside, Rails templates, and generators in general, are super powerful. I'm a teacher and my students will typically create 15 to 20 rails projects through the course, and providing them with a good template file with debugging gems, spec style testing, etc. is a huge timesaver. After they've made the changes once themselves, of course. If you're interested, my personal
.rails-template.rb
is on GitHub.对于 Rails 4,请注意,或者如果您使用“turbolinks”、“uglifier”或任何其他需要服务器解释 javascript 的 gem,也请将它们注释掉。
Note for Rails 4, or if you're using 'turbolinks', 'uglifier', or any other kind of gem that requires the server to interpret javascript, comment them out as well.
我遇到了这个问题,因为我正在使用 codekit 来编译我的咖啡脚本。
我通过将“assets/coffee”文件夹重命名为“assets/cafe”来解决这个问题,这样rail就找不到它了。
编辑:什么有用(唯一对我有用的东西,上面的答案不起作用)是添加一个单独的文件夹“App/Coffee”,并将其设置为编译到 asset/javascript 文件夹中。如果它在 asset 目录中,则无论名称如何,rails 都会找到它。
I had this problem, as I am using codekit to compile my coffeescript.
I got around it by renaming my 'assets/coffee' folder to 'assets/cafe', so rail wouldn't find it.
Edit: What does work (and the ONLY thing that works for me, the above answer does not work) is to add a separate folder 'App/Coffee', and setting it to be compiled into the assets/javascript folder. If it's in the assets directory, rails will find it no matter the name.