Topic: using ajax and erb to ease delayed rendering of a view

hi, i'm relatively new to ror and even more new to ajax so i'm not sure this is even possible but here goes.

when users submit info to my app, it kicks off a lengthy process that they wait for to complete, which delays the next view from rendering. to avoid a delay in rendering the view, i'd like to render a view immediately that says "please wait while your results are generated..." the view in the meantime would use ajax to start the lengthy process. when the process is complete, a div in the view would be updated using erb and instance variables from the controller's action to show results of the lengthy process. something like this for example:

<script language="javascript">
feedback = new Ajax.Updater( 'feedback', '/answer/feedback',
                    {    asynchronous:true,
                         oncomplete:
                                         method: post
                    });
</script>

<div id="feedback">
please wait while your results are generated...
</div>


what i'm trying to find out is, is it possible to stick erb in a div element after process completion and will it get evaluated? if that is possible could you point me to an example that solves this kind of scenario? and what are typical solutions to long process scenarios like this? if there are other ways to handle this i'd appreciate hearing them as well. thanks for any replies.

Re: using ajax and erb to ease delayed rendering of a view

You have a wrong viewpoint about how this works.
1) user klicks link for ajax action
2) Show the div "please wait"
3) link calls action through AJAX
4) action is run
5) at the end of the action, you just render a partial for the div with the results through it. in that partial you can use ERb like in any other.

#view:
link_to_remote "Start Process", :url => { :action => "do_somthing" },
                                :before => "$('feedback').show"
                                :update => "feedback"
#controller:
def do_something
  # do your lengthy procedure
  render :partial => "results"
end

The render code would look a bit different if you intended to not only replace the content of one div, but do also other stuff on the page, but for this one update, it's fine.

Last edited by Duplex (2007-10-22 19:35:47)

Re: using ajax and erb to ease delayed rendering of a view

thanks for the pointers, i appreciate it. i was hoping to avoid making the user click a link when they got to the final feedback view page by automatically starting the lengthy process, but this seems to be ok too. thanks again.

Re: using ajax and erb to ease delayed rendering of a view

you don't have to use a link, was just an example ...

link_to_remote does nothing more that create a link that calls some jaascript.
If you know Prototype well enough, you can writer that Code yourself and just put it in the window.onload event or something ....