Ajax/forms confusion

Some time ago I asked about the
use of javascript “please wait” messages whilst a big query runs. Now I
am back to working on this code I need to deal with this problem and
have had no luck finding anything that works.

The setup is as follows:

  1. A search_form action prepares a complex form and submits a large
    number of parameters to…
  2. A big_query action which takes some time to run and then renders its
    big_query view.

Until (2) has finished running users see the view of (1) with no clue
that anything is actually happening. So, ideally some sort of javascript
that would clear away the form in (1) and replace with the waiting
message the moment the “submit” button is clicked would suit. But, I
can’t find any means of both submitting the form and running the
javascript. Perhaps it is form_remote_tag, but the syntax for this is
not at all clear.

Perhaps something like this in search_form.rhtml:

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

Any clarification as to how this should work would be most welcome -
thanks.

Milo T. 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):

<% 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 %>

Arguments are the same as link_to_remote.

Mark B. wrote:

<% 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 %>

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.

On 26 Mar 2008, at 11:02, Milo T. wrote:

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.

On Mar 26, 7:02 am, Milo T. [email protected]

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs