Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coffeescript and Haml with unobtrusive Javascript (data-remote) in Rails 3.1

I searched le interwebs, but I haven't found someone experiencing the same problem as me, so I propose my question here.

I just started using Rails 3.1 with Compass, Haml and CoffeeScript and ran into a problem. When I rename my controller-specific JavaScript file located in app/assets/javascript/index.js to index.js.coffee and translate the JavaScript code to CoffeeScript, everything works as expected - the file is requested by the browser and compiled on the fly into JavaScript. Changes in the CoffeeScript file do also trigger recompilation.

However, when I try to do this with unobtrusive JavaScript (:remote => true) and rename the already working JavaScript file located in the view folder app/views/index/index.js.haml to index.js.coffee.haml and translate the included code, Rails doesn't recognize it as a CoffeeScript that needs to be compiled.

What am I doing wrong? Do I actively have to enable CoffeeScript evaluation for the view? Where?

like image 309
squidfunk Avatar asked Nov 25 '11 12:11

squidfunk


1 Answers

The asset pipeline introduced in Rails 3.1 will automatically compile coffeescript assets into javascript for you, as you've mentioned. This is currently NOT the case for views: only files in app/assets/javascripts will be compiled.

To use Coffeescript in views, you'll need to use https://github.com/markbates/coffeeBeans for the time being.

Update : per Kyle Heironimus' comment below, it seems this functionality can be provided by coffe-rails (see https://github.com/rails/coffee-rails and http://rubygems.org/gems/coffee-rails )

like image 147
David Sulc Avatar answered Oct 05 '22 05:10

David Sulc