Luke Melia

software dev

January 9, 2006

Debugging your RJS calls in Rails

RJS templates aka JavascriptGenerator templates are a powerful tool in the latest versions of the Ruby on Rails framework that has been a real mind-bender for me. In a nutshell, they give you the ability to respond to an AJAX request by telling the page to execute a sequence of javascript commands. And the javascript can be generated using Rails’ excellent helper classes that build on prototype.js and scriptaculous.

Turns out they can be a little hairy to debug. When I had some bad javascript, I wasn’t seeing it captured in the FireFox console. The javascript would just silently fail to execute if there was invalid javascript syntax. Rails Weenie to the rescue:

Drop this into the bottom of your layout:

<div id="debug">
</div>
<script type="text/javascript">
	Ajax.Responders.register({
	// log the beginning of the requests
	onCreate: function(request, transport) {
	new Insertion.Bottom('debug', '<p><strong>[' + new Date().toString() + '] accessing ' + request.url + '</strong></p>')
	},
	
	// log the completion of the requests
	onComplete: function(request, transport) {
	new Insertion.Bottom('debug',
	'<p><strong>http status: ' + transport.status + '</strong></p>' +
	'<pre>' + transport.responseText.escapeHTML() + '</pre>')
	}
	});
</script>

This let’s you see what ajax calls are being made and what javascript is being sent in response. Notice your boneheaded error, fix it, and smile!

Leave a Reply

LukeMelia.com created 1999. ··· Luke Melia created 1976. ··· Live With Passion!