Fun with Rails on the edge and RJS

November 18, 2005 Link to post  Permalink

There’s a new template system being built into Rails right now that helps generate Javascript responses to browser Ajax calls. The template extension is RJS.

Since I wrote a bunch of code to do something like this only yesterday, today, I sync’d up to the latest Rails to give it a go.

Well, it works! Reading the RDoc comments in the source code, and with a little experimentation, I got the thing to work, converting my rhtml file into the rjs equivalent.


I found a bug in Prototype. Prototype will detect a Javascript return and run it automatically, without you having to setup a special handler. This code detects the Content-type HTTP header and looks for text/javascript and then runs the contents of the response.

The problem is, it checks for exactly ‘text/javascript’, but WEBrick on my Windows machine returns ‘text/javascript; charset=UTF-8’, so the auto eval() doesn’t fire.

Looking at the development log file, it looks like this method also runs faster than generating almost the same javascript in an rhtml file. Instead of seeing 30 requests/sec in the log, I’m seeing 60 requests/sec. I’m figuring this is due to the template file being pre-compiled and cached.

This type of functionality is why I love this Rails thing, always something new, and it’s always relevant to what I’m building.