# Double layer of AJAX?

**URL:** <https://www.ruby-forum.com/t/double-layer-of-ajax/78537>\
**Category:** Rails\
**Created:** [December 6, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/double-layer-of-ajax/78537 "2006-12-06T19:37:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/double-layer-of-ajax/78537/1 "2006-12-06T19:37:37Z")

</div>

Is it possible or easy to have a double layer of ajax ?

1. Basicaly there is a page that is rendered where you can delete  
objects by marking them with a check box. When you click delete all  
marked, it deletes those objects and updates the page with ajax.

2 part 1 above is followed by another section where if you click on  
“update” it displays a upload form through ajax for acts\_as\_attachment  
where the user can browse for a file to upload.

1 & 2 are both ajax, but after 2 runs it has to redisplay the page. If  
there was another ajax layer that rendered both 1 & 2, and 1 & 2 where  
also ajax, then the ajax would have 2 layers. Is that possible or easy  
to do ?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 6, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/double-layer-of-ajax/78537/2 "2006-12-06T22:34:32Z")

</div>

On 12/7/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> where the user can browse for a file to upload.
> 
> 1 & 2 are both ajax, but after 2 runs it has to redisplay the page. If  
> there was another ajax layer that rendered both 1 & 2, and 1 & 2 where  
> also ajax, then the ajax would have 2 layers. Is that possible or easy  
> to do ?

If I understand you correctly, you want to reload the page after  
completing the file upload, right? That’s what RJS is for. RJS is a  
way of sending javascript commands back to the browser as the response  
of an Ajax request, which then gets executed by the browser instead of  
displayed as html.

You can create a template with extension .rjs, for example  
‘upload.rjs’ which is rendered after the uplaod action is complete and  
redirect back to the original page. This file should look something  
like this:

page.redirect :controller=\>‘some\_controller’, :action=\>‘show’,  
:id=\>@some\_model

Cheers,  
max

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 7, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/double-layer-of-ajax/78537/3 "2006-12-07T15:49:02Z")

</div>

I have some ajax working, and it renders part of the page. What I  
really am asking is:

say you have a page A that partially renders B so that A does not  
refresh when the action for B is invoked. However now you have C.  
C is either part of B or B and C are part of something else. When the  
action for C is invoked which is a acts\_as\_attachment new. It renders C  
without refreshing B. This rendering of C shows an upload form. After  
the upload occurs another refresh would happen which would refresh B  
and C but not A. It’s a little complex and that’s the best I can  
explain it. I’m a little confused about it myself …

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 7, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/double-layer-of-ajax/78537/4 "2006-12-07T16:31:16Z")

</div>

wbsurfver wrote:

> explain it. I’m a little confused about it myself …  
> State it again in terms of what a user would perceive.

(That technique is the power behind Use Cases…)

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 7, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/double-layer-of-ajax/78537/5 "2006-12-07T21:46:58Z")

</div>

Ok, there is a main page, call it A. On this page there is a set of  
pictures (call this B) you can mark each picture for delete with check  
boxes and click a delete which will delete them and update the page so  
that only B is refreshed by removing the deleted pictures and re  
rendering B without rendering A. Then there is another section (call  
that C). Here there is a link that says “upload picture”. This is done  
because acts\_as\_attachement seems to work in that first there is a call  
to new() to create an item in the controller. so you click “upload  
picture” which calls new(), new() renders a new ajax through C without  
refreshing A or B. Now instead of seeing “upload picture” as a link,  
you see a browser form where you can select an image from the disk.  
This was rendered without refreshing A or B and is how  
acts\_as\_attachment works from examples I have followed. Next when you  
send the image back to the controller that you selected in the browser  
it’s through a create() call as acts\_as\_attachment works. After the  
create call, my code refreshes the whole page A,B, and C. I want it to  
just refresh B and C.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 8, 2006, 3:57am UTC](https://www.ruby-forum.com/t/double-layer-of-ajax/78537/6 "2006-12-08T03:57:41Z")

</div>

On Dec 7, 2006, at 12:45 PM, removed\_email\_address@domain.invalid wrote:

> to new() to create an item in the controller. so you click “upload  
> picture” which calls new(), new() renders a new ajax through C without  
> refreshing A or B. Now instead of seeing “upload picture” as a link,  
> you see a browser form where you can select an image from the disk.  
> This was rendered without refreshing A or B and is how  
> acts\_as\_attachment works from examples I have followed. Next when you  
> send the image back to the controller that you selected in the browser  
> it’s through a create() call as acts\_as\_attachment works. After the  
> create call, my code refreshes the whole page A,B, and C. I want it to  
> just refresh B and C.

```
You cannot do file uploads via ajax. That being said , you can do

```

what looks like ajax file uploads by targeting the upload form to  
submit to a hidden iframe on the page. But then responding back from  
the upload controller without refreshing the entire page is hard  
because whatever you return from the upload action goes to the iframe  
where the upload was sent thru. So you need a plugin called  
respond\_to\_parent that will let you send an rjs response back to the  
iframe that will get eval’d in the parent window so you can refresh  
just a small part of the page.

```
Phew! confused yet?

```

[http://sean.treadway.info/svn/plugins/responds\_to\_parent/README](http://sean.treadway.info/svn/plugins/responds_to_parent/README)

Cheers-  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)
