# Javascript "please wait" image

**URL:** <https://www.ruby-forum.com/t/javascript-please-wait-image/116753>\
**Category:** Rails\
**Created:** [October 9, 2007, 12:14pm UTC](https://www.ruby-forum.com/t/javascript-please-wait-image/116753 "2007-10-09T12:14:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [October 9, 2007, 12:14pm UTC](https://www.ruby-forum.com/t/javascript-please-wait-image/116753/1 "2007-10-09T12:14:19Z")

</div>

When I used to write Perl CGI scripts I displayed an animated gif image  
whilst waiting for a long database query to load, and some javascript to  
remove the image once the content was available.  
I’d like to do the same thing with Rails, but it’s not clear to me how  
to do this in Rails. Does anyone have any suggestions?

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [October 9, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/javascript-please-wait-image/116753/2 "2007-10-09T12:28:07Z")

</div>

Milo T. wrote:

> When I used to write Perl CGI scripts I displayed an animated gif image  
> whilst waiting for a long database query to load, and some javascript to  
> remove the image once the content was available.  
> I’d like to do the same thing with Rails, but it’s not clear to me how  
> to do this in Rails. Does anyone have any suggestions?

in rails-ajax you have a :loading =\> “Jsfunct()”, :complete =\>  
“Jsfunct2()” for all of the prototype based js-rails things.

you can do something like (given you have the :defaults js files  
included in the )  
DOM

* * *

 ![]()

The ajax call will replace this div

link\_to\_remote “load query”,  
:url =\> {:controller=\>‘mycontroller’, :action=\>‘queryloader’},  
:update =\> “dom\_updater”,  
:loading =\> “Element.show(‘spinner’)”,  
:complete =\> “Element.hide(‘spinner’)”

class cController

def queryloader  
BigModel.find(:all) # make long query  
render\_text “this will be replaced in the div called dom\_updater!”  
end

end

* * *

…the img is actually placed on the page at start but with display:none.  
when the ajax call is made, the :loading opt loads a js function (it  
could be any function u define, i chose the simple prototype  
Element.show/hide) that displays / hides the img according to the  
process of the action in the background.

hope it helps!  
s

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [October 9, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/javascript-please-wait-image/116753/3 "2007-10-09T12:41:47Z")

</div>

Shai R. wrote:

> …the img is actually placed on the page at start but with display:none.  
> when the ajax call is made, the :loading opt loads a js function (it  
> could be any function u define, i chose the simple prototype  
> Element.show/hide) that displays / hides the img according to the  
> process of the action in the background.

That looks a lot better than my crude Perl methods! I will give it a go,  
but I’m not sure whether it will work with my odd setup (can’t test  
right now, whilst I populate the database).  
The search button submits a form, calling the search method. If there is  
anything found by the search then it stores the results in the session  
and re-directs to a “view results” page. So, the “search” page is never  
actually rendered.
