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?

K Everest
  • 1,565
  • 5
  • 15
  • 23

5 Answers5

61
  1. Comment out gem "coffee-script" in your Gemfile
  2. Use .js instead of .js.coffee for your javascript files
Gaurav Gupta
  • 5,380
  • 2
  • 29
  • 36
  • 2
    Is this documented somewhere that commenting out the `coffee-script` gem is enough? – K Everest Jan 04 '12 at 14:05
  • 4
    Why don't you just try it out? – Gaurav Gupta Jan 05 '12 at 03:53
  • 1
    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. – Paul A Jungwirth Apr 25 '12 at 21:04
  • 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) – Kevin Apr 18 '13 at 22:21
  • 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. – Justin C Aug 19 '15 at 00:25
  • 2
    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. – SalientGreen Sep 23 '15 at 14:50
  • 3
    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 }` – codehugger Nov 04 '15 at 10:07
  • `config.generators` seems like the right way to do it. – R891 May 09 '16 at 02:09
  • Before remove from gemfile, execute rake tmp:cache:clear – Milton Quirino Aug 11 '17 at 18:25
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
Koen.
  • 25,449
  • 7
  • 83
  • 78
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.

Dan
  • 2,952
  • 4
  • 23
  • 29
-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.

Dominic
  • 106
  • 4
-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.

Kevin
  • 4,225
  • 2
  • 37
  • 40
  • 2
    Damn, scratch that after restarting rails it's found assets/cafe and is trying to compile it again. – Kevin Sep 24 '12 at 17:02