I found a plethora of examples on the internet, but nothing that walked me from start to finish through using a RJS template in Rails. I hope that someday the following saves someone else from that void.
Take any old Rails application and add a time controller to it.
# script/generate controller time index exists app/controllers/ exists app/helpers/ create app/views/time exists test/functional/ create app/controllers/time_controller.rb create test/functional/time_controller_test.rb create app/helpers/time_helper.rb create app/views/time/index.rhtml
The index action will be the one that Rails serves up when you head to the /time url. Modify the app/views/time/index.rhtml to look like:
class TimeController < ApplicationController def index end def now end end
The controller actions are anything but complicated, but now we need that action to return something. Usually the action looks for app/views/time/now.rhtml but execute and run app/views/time/now.rjs if it does not find it. Don't be silly and try to name your RJS templates the same as some other usable action within your application. Create app/views/time/now.rjs to read
page.replace_html 'current_time', :partial=>'time/current'
This template takes the current page, and replaces the current_time element with whatever is returned from the time/current partial. The partial is rendered from both the RJS template and the index.rhtml template is to ensure the DRYness of our logic. The app/views/time/_current.rhtml template can be as simple as
<%= Time.now().to_s %>