So, I had need of a little ‘Ajax’. I wanted to provide a brief preview of some content, but without all the nastiness of page refreshes – an ideal candidate for XMLHTTPRequest. So I wrote my own. That was a mistake.
I’m comfortable with Javascript – I think I’m fairly accomplished with it – so I got it done, and it worked. It was only then that I thought ‘I think Rails does Ajax too’. Does it ever! Ten minutes, and I had a Rails Ajax version working (a lot less than my own time, even with my boiler-plate code) and, and this is the best bit, much more readable code! That’s what I call a win. link_to_remote was the useful function in question, and my call is:
link_to_remote( "Show",
:url => { :action => "show_small", :id => game.id },
:update => "js_board",
:before => "document.getElementById('js_board').style.left = get_x(this);
document.getElementById('js_board').style.top = get_y(this)",
:complete => "document.getElementById('js_board').style.visibility = 'visible'" )