If you have access to Agile with Rails v. 2, it has a good tutorial on what’s called RJS templates. Basically, you use Ruby to write out javascript that gets executed on the client (the {executeScripts: true} of Prototype).
Some info is also available on the api page: http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M000559
You can write RJS in your controller or as a partial
_partial_name.rjs
Note: if you do this, you must not use :update, or the resulting javascript will be printed to the page.
Jason