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

1

11

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?

Cornstalk answered 25/11, 2011 at 12:20 Comment(1)
See what happens if you switch it to using ERB instead. Considering how HAML works, I can't see it working well with CoffeeScript or even javascript for that matter.Minneapolis
P
6

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 )

Philanthropy answered 25/11, 2011 at 15:36 Comment(6)
Thanks! This seems to be one step in the right direction, but it doesn't solve my problem entirely. I can now rename my file to index.js.coffee and it will be correctly processed as CoffeeScript. However, I need to parse it with HAML first, because I need to assign variables. When I tried index.js.coffee.haml, Rails 3.1 still doesn't recognize the template. Is it even possible? Or is there another way to pass variables from a controller to CoffeeScript without having to convert them to JSON using HAML?Cornstalk
Okay, upon reading this comment on a related problem, I decided not to use CoffeeScript in the view, and thus stick to index.js.haml. Has anyone had any performance issues by passing Ruby variables to JS templates in unobtrusive Javascript?Cornstalk
wait, wait, wait, Did you just say you're sticking with index.js.haml? I think that according to the comment you just mentioned, the RIGHT way to go is to use the index.js.coffee NOT index.js.haml! .haml assumes you'll still be rendering javascript within a HAML file, Whereas .js.coffee would be pure unobtrusiveness especially if you use HTML 5's data tags that pretty much everyone suggests these days. Could you care to explain your decision. It has kind of confused me.Dakotadal
No, the reason why I decided to fall back to plain Javascript/Haml was the fact that I am unsure whether Haml (or Slim which I use nowadays) re-compiles the Coffeescript code every request, so performance-considerations. However, the current application I am building doesn't use the unobtrusive Rails/JS driver, since I have to handle a lot of stuff before/after deploying the request. Therefore I now transfer the whole JS code to the browser upon page loading. Maybe someone can shed light on the re-compilation issue?Cornstalk
Oh it's coming back to me ;) Another problem was the bypassing of the asset pipeline when returning JS/Coffee views - so no minification/scrambling!Cornstalk
This should be supported in rails by default now using coffee-rails.Excaudate

© 2022 - 2024 — McMap. All rights reserved.