How can I completely disable CoffeeScript in a Rails 3.1 app?
Asked Answered
A

5

40

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?

Aidoneus answered 4/1, 2012 at 4:54 Comment(1)
You can get a list of all coffeescript files with find . -iname '*.coffee'.Paronymous
S
61
  1. Comment out gem "coffee-script" in your Gemfile
  2. Use .js instead of .js.coffee for your javascript files
Stereobate answered 4/1, 2012 at 5:27 Comment(9)
Is this documented somewhere that commenting out the coffee-script gem is enough?Aidoneus
Why don't you just try it out?Stereobate
I don't have that line in my Gemfile, but I still needed to install a JS engine in production to compile assets (using Capistrano with the load 'deploy/assets' line). Sort of annoying.Piliferous
No, removing the gem from your gemfile does NOT work. Keeping your cofeescript outside 'assets' seems to be the only way (which is utterly frustrating)Fm
In Rails 4, removing coffee-rails may result in cannot load such file -- coffee_script errors due to stale coffee files in the tmp/cache/assets folder. Run rake assets:clobber to remove them.Disordered
In general, removing the item from the Gemfile should work, but there are caveats. coffee-script may be a dependency of another GEM. The best place to check for dependencies is ./Gemfile.lock. For example: coffee-rails is a dependency of turbolinks, and coffee-script is a dependency of coffee-rails. So, if you remove gem 'coffee-script' from the Gemfile, but if you have gem 'turbolinks'', CoffeeScript will still be installed.Heartwarming
also if you use something like active_admin, coffescript is included as dependency there as well so this does not only apply to standard rails gems. Safest to use config.generators do { |g| g.javascript_engine :js }Senility
config.generators seems like the right way to do it.Decry
Before remove from gemfile, execute rake tmp:cache:clearSawn
H
28

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
Hyland answered 8/4, 2014 at 10:8 Comment(6)
You can do this on one line, too: config.generators.javascript_engine :jsCantoris
This no longer appears to be necessary in Rails 4.1. Simply removing 'gem coffee-rails' from the Gemfile is enough.Flannel
It is still necessary even in Rails 4.1 if you have a transitive dependency on coffee-rails such as activeadminBenniebenning
This no longer necessary in Rails 4.2.4Lazarus
It's necessary even in Rails 4.2.6. Without that, it generates coffeescript files.Decry
this should be the top answerDilute
N
1

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.

Nugatory answered 1/7, 2017 at 17:59 Comment(0)
S
-1

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.

Shaving answered 11/7, 2013 at 16:59 Comment(1)
You may still want turbolinks or uglifier, even if you aren't still using CoffeeScript in your own source code.Flub
F
-4

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.

Fm answered 23/9, 2012 at 0:12 Comment(1)
Damn, scratch that after restarting rails it's found assets/cafe and is trying to compile it again.Fm

© 2022 - 2024 — McMap. All rights reserved.