how to DRY: ajax action +initial page load

I must be missing something, because I don't get a clean way how to do this.

I have a page which when you change the contents of a select, changes the contents of a div by calling an action.

How do I have the contents of the div be the results of calling the action on the initial page load? I understand if I wanted to render just the template I could use partials, but I also need to have the action function called.

NOTE: the main action should not depend on whether the updatable piece is included on the page.



def main   ...stuff...   # should not care if page has a change_piece on it, so should not need to do change_piece stuff end

def change_piece   ...stuff that must happen...   when called by ajax:     render :update do |page|       page.replace_html :bob, :partial => "piece"           end          when called as part of initial page load:     render :partial => "piece" end

view (main):

<html> ...stuff... <div id="bob"> <%= render :action "change_piece" %></bob> ... </html>

I tried using render_component, with a respond_to block in the action, but it just renders the first thing in the respond_to block. IE, if I have format.html first it renders that first. If I have format.js first, it renders that. (the respond_to block works correctly otherwise)

Thanks, Michael Johnston