Ajax/forms confusion

Milo Thurston wrote:

<%= form_remote_tag({ :action => 'big_query', :loading =>
"Element.show('spinner')", :complete => "Element.hide('spinner')",
:update => "hide_me"}) %>

The values of the :loading and :complete keys are just JavaScript, so
you can have any amount of stuff in there. The following should work
(not tested it, though):

<div id="hide_me">
<% form_remote_tag :url => {:action => :big_query},
    :loading => "Element.hide('hide_me'); Element.show('spinner')",
    :complete => "Element.hide('spinner'); Element.show('hide_me')",
    :update => "hide_me" do %>
form stuff in here
<% end %>
</div>

Arguments are the same as link_to_remote.

Mark Bush wrote:

<div id="hide_me">
<% form_remote_tag :url => {:action => :big_query},
    :loading => "Element.hide('hide_me'); Element.show('spinner')",
    :complete => "Element.hide('spinner'); Element.show('hide_me')",
    :update => "hide_me" do %>
form stuff in here
<% end %>
</div>

Thanks, that's a lot better than I've been getting so far.
The only problem seems to be that the app wants to render everything
within the same page afterwards, so if the user is re-directed (e.g. to
login) then the page ends up looking rather odd. That's presumably my
lack of understanding of how rendering works, though.

That is an unfortunate truth: the browser hides the behind the scenes redirect from the javascript (at least in firefox it does) so it just sees the final result without knowing that a redirect has happened.

Fred

@Milo -- It may be obvious to you know, but when you use
form_remote_tag the form expects javascript back presumably with the
intention of changing one or more elements on the page. If your
"big_query" that renders a "big_view" was previously using a full
postback then you need another solution. Maybe some js hooked to the
button (onclick) or form (onsubmit)?

submit_tag "Start big query", :onclick=>"function(){$
('spinner').show(); return true;}"

The key is making sure you return true. That lets the form know that
the click still needs the default handling available from the form. I
think you should be able to do something like this to display a 'wait'
message; the view rendering or re-direct will take care of replacing
the page when it's ready.

AndyV wrote:

If your
"big_query" that renders a "big_view" was previously using a full
postback then you need another solution.

What I did was move big_query.rhtml to _big_query.rhtml and added
"render_partial" to the end of the action in the controller. This seems
to do what is required, but I'll give your suggestion a try as well.