Today’s gotcha: CoffeeScript templates in Rails work in development but not production by default

January 11, 2012 | Bug of the Day, Development, Thoughts from the team

Deployment is always a bit of a pain. No matter how hard you try there will almost always be a subtle difference between your dev and production environments (which is why ideally you should have a deployment test environment).

I had an odd bug today when deploying a Rails app.  I have an AJAX call, that returns a javascript block.  The template is actually a CoffeeScript file with the name, the idea being that the CoffeeScript gets compiled to JavaScript before being sent out.

This works absolutely fine in development mode on my Mac.

Deploy it to the server and I get

ActionView::MissingTemplate (Missing template projects/check, application/check with {:handlers=>[:erb, :builder], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :obj], :locale=>[:en, :en]}. Searched in:
* "/home/.users/70/revedadmin/online_editor/app/views"
* "/usr/local/lib/ruby/gems/1.9.1/gems/devise-1.5.0/app/views"
* "/usr/local/lib/ruby/gems/1.9.1/gems/kaminari-0.12.4/app/views"

The application is looking for check.js.erb or something similar.  It doesn’t know what to do with the CoffeeScript file.

It turns out that Rails won’t compile CoffeeScript templates by default, but you can install the Coffeebean gem and it magically works. It also has view helpers which let you drop CoffeeScript into an erb template like this…

<%= coffee_script_tag do %>
  alert 'coffee script is awesome!'
<% end %>

which is neat.

But the real gotcha is that this works out of the box in development mode, so you assume that it will work in production.

When in development mode, the asset compilation pipeline is appears to be compiling the CoffeeScript template at runtime. But with compilation turned off in production mode this no longer happens.

I assume this is not an intended behaviour as it rather breaks the idea of Rails apps working the same largely irrespective of the platform.  I will do some more digging.

More like this