I have created a Rails3 application, started with Haml/Sass and finding it awesome. I am also trying to do unobtrusive javascript.

Before, in Rails 2.3, I would have expected a remote-form to have an :update attribute, where you could specify a selector where the response of the remote method would be rendered.

Now it needs to be done differently: my controller function will render a “.js” view, which will do the necessary actions itself. This makes it more library agnostic, and i prefer jquery.

So for instance i have a controller action “search”, and i have a corresponding “search.js.erb” as follows:

$('.grid').replaceWith('<%= escape_javascript(render :partial => 'list') %>')

This works. It will replace the html of the element with a class grid with the html from my rendered partial.

I started out trying to achieve that in HAML, and failed at first. So first i created the above ERB code which worked. Now my task seemed simpler: translate this seemingly simple line to HAML.

My first naive approach was to do the following:

= "$('.grid').replaceWith('#{escape_javascript(render :partial => 'list')}')"

But this just places the html as a readable string inside my page. Even i do something simple like

= "$('.grid').replaceWith('<h3>Text</h3>')

i see the actual characters

<h3>Text</h3>

and not the markup. But i need to contain the javascript inside the double-quotes or haml will not recognise it, and can not interpolate my ruby there.

After looking through the HAML reference, actually the solution was incredible simple. Once more.

!= "$('.grid').replaceWith('#{escape_javascript(render :partial => 'list')}')"

The != unescapes HTML (as opposed to the standard =). This is exactly what we need of course.