Ajax and RJS templates

RJS templates allow a script of client-side actions to occur within one request. A controller action calls it like a view. Here is a simple example using the Time demo:

  <%= javascript_include_tag :defaults %>
  <h1>Time Ajax Demo</h1>
  <p><%= link_to_remote 'Get Current Time', :url => {:action => 'refresh'} %>
  </p>
  <p id='currentTime'>
  Current time will appear here
  </p>

The refresh request then uses the following code found in refresh.rjs within the views folder:

  page.replace_html 'currentTime', "Current time is #{Time.now.to_s}"
  page[:currentTime].visual_effect :highlight

This time, the server is sending javascript code (created from ruby methods) to the browser. The browser then executes the code.