如何在 Rails 3.1 应用程序中完全禁用 CoffeeScript?

发布于 2024-12-24 03:16:30 字数 190 浏览 1 评论 0原文

当我生成一个新的控制器时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅忆流年 2024-12-31 03:16:30
  1. 在 Gemfile 中注释掉 gem "coffee-script"
  2. 在 JavaScript 文件中使用 .js 而不是 .js.coffee
  1. Comment out gem "coffee-script" in your Gemfile
  2. Use .js instead of .js.coffee for your javascript files
等风来 2024-12-31 03:16:30

不确定这对于 Rails 3.1 是否有效,但在 4 中,您还应该将 application.rb 中的 javascript_engine 设置为 :js 以指示生成器创建.js 文件而不是 .js.coffee

config.generators do |g|
  # .. other configuration ..
  g.javascript_engine :js
end

Not sure if this counts for Rails 3.1 but in 4 you should also set the javascript_engine to :js in application.rb to instruct generators to create .js files instead of .js.coffee.

config.generators do |g|
  # .. other configuration ..
  g.javascript_engine :js
end
仅此而已 2024-12-31 03:16:30

Koen 和 Gaurav Gupta 给出了很好的答案!

如果您想对每个新的 Rails 项目自动进行这些更改,您可以使用模板文件。

~/rails-template.rb

# Don't install coffeescript
gsub_file 'Gemfile', /^gem \'coffee-rails\'/ do
  "\# gem 'coffee-rails'"
end

# Mess with generators to get the behavior we expect around new files
# For these injections, indentation matters!
inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do
  <<-'RUBY'
    config.generators do |g|
      # Always use .js files, never .coffee
      g.javascript_engine :js
    end
  RUBY
end

然后在 ~/.railsrc

-m ~/.rails-template.rb

现在每当你运行 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

# Don't install coffeescript
gsub_file 'Gemfile', /^gem \'coffee-rails\'/ do
  "\# gem 'coffee-rails'"
end

# Mess with generators to get the behavior we expect around new files
# For these injections, indentation matters!
inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do
  <<-'RUBY'
    config.generators do |g|
      # Always use .js files, never .coffee
      g.javascript_engine :js
    end
  RUBY
end

Then in ~/.railsrc

-m ~/.rails-template.rb

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.

深海蓝天 2024-12-31 03:16:30

对于 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.

手长情犹 2024-12-31 03:16:30

我遇到了这个问题,因为我正在使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文