# Alternative to render :update

**URL:** <https://www.ruby-forum.com/t/alternative-to-render-update/164890>\
**Category:** Rails\
**Created:** [April 7, 2009, 3:45am UTC](https://www.ruby-forum.com/t/alternative-to-render-update/164890 "2009-04-07T03:45:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 7, 2009, 3:45am UTC](https://www.ruby-forum.com/t/alternative-to-render-update/164890/1 "2009-04-07T03:45:26Z")

</div>

Hi guys and gals.

I don’t know how to explain this too well, so I’ll try my best and post  
some obviously dysfunctional code (just so you can get the idea of what  
I’m after if my explanation doesn’t do it.)

What I want to do is create an upload form that users can upload more  
than one item with. The catch is I want to use ajax to pull off an  
insert\_html and update the page showing thumbnail to each file as it’s  
added, while the other files are being uploaded. So, let’s say I upload  
3 files, when the first one finishes it’s inserted to an unordered list  
(via ajax, without reloading the page while the other two files are  
still uploading), then the next file, then the last file and it ends.  
I’ve been doing it fine with a single file, but now that I’ve added  
multiple file uploads that controller style won’t work.

Doing something like this, which I figured was a 1,000,000:1 shot at it  
working (of course, it didn’t) gives a DoubleRender error which I  
expected:

def create  
@project = Project.find(params[:id])

params[:attachment].each do |key, value|  
if value and value != ‘’  
@file = Image.new(Hash[‘uploaded\_data’ =\> value])  
@file.user\_id = logged\_in\_user.id  
@file.project\_id = @project.id

```
  respond_to do |format|
    if @file.save
      format.js do
              responds_to_parent do
                render :update do |page|
            page.insert_html :top, "images", :partial => 'image',

```

:object =\> @file  
end  
end  
end  
else  
# etc…  
end  
end  
end  
else  
# etc.  
end

Can this sort of thing even be pulled off?

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 7, 2009, 4:36am UTC](https://www.ruby-forum.com/t/alternative-to-render-update/164890/2 "2009-04-07T04:36:28Z")

</div>

You want to execute multiple inserts in a single response, not  
multiple responses. This might work, assuming that the rest of your  
code works and that you are using attachments as the relationship  
name.

def create  
@project = Project.find(params[:id])

params[:attachment].each do |key, value|  
if value and !value.blank?  
new\_file = Image.new(Hash[‘uploaded\_data’ =\> value])  
new\_file.user\_id = logged\_in\_user.id  
@project.attachments \<\< new\_file  
end  
end

respond\_to do |format|  
if @project.save  
format.js do  
render :update do |page|  
@project.attachments.each do |file|  
page.insert\_html :top, “images”, :partial =\>  
‘image’, :object =\> file  
end  
end  
end  
end  
end  
end

On Apr 7, 9:45 am, Jack B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 7, 2009, 4:04pm UTC](https://www.ruby-forum.com/t/alternative-to-render-update/164890/3 "2009-04-07T16:04:03Z")

</div>

Hi Jaryl, thanks.

That would work, but it has a slightly different behavior than what I’m  
looking for. I want it to begin the upload of the multiple images, let’s  
say 3, and when one finishes being processed (resized, etc.) it’s  
inserted into the page and then the second image starts being processed  
and inserted, then the third. Your solution would process all three and  
after it finishes processing it’ll dump all 3 of them out at essentially  
the same time.

It’s a relatively unimportant difference, but I was wondering if there’s  
a way to pull that off.

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 8, 2009, 12:19am UTC](https://www.ruby-forum.com/t/alternative-to-render-update/164890/4 "2009-04-08T00:19:08Z")

</div>

Google for a Rails version of swfupload… in the Rails code it updates  
the thumbnails as it processes the files.

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 8, 2009, 7:49am UTC](https://www.ruby-forum.com/t/alternative-to-render-update/164890/5 "2009-04-08T07:49:58Z")

</div>

If that’s the case, then you should split it up into multiple  
requests. I believe that you can only send back one response per  
request, so that is an inherent problem.

Perhaps you may try sending a request once the a valid file is  
selected in the file upload field.

On Apr 7, 10:04 pm, Jack B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 8, 2009, 3:27pm UTC](https://www.ruby-forum.com/t/alternative-to-render-update/164890/6 "2009-04-08T15:27:49Z")

</div>

Jaryl Sim wrote:

> If that’s the case, then you should split it up into multiple  
> requests. I believe that you can only send back one response per  
> request, so that is an inherent problem.

Sure that’s the case, that’s how HTTP protocol works. One response for  
one request. I think of it as similar to an event loop, where each cycle  
through the loop processes one event.

The difference is that an event loop is continuous, where a  
request-response cycle sleeps between each cycle. In other words the  
event loop runs continuously checking an event queue, where as HTTP  
sleeps until it is sent a request, which causes it to “awake” and  
process the request and generate a response. After the response is  
processes it sleeps again until awoken by another request.

> Perhaps you may try sending a request once the a valid file is  
> selected in the file upload field.

As far as I know, the behavior you’re after is going to require some  
client-side code. I know there are a number of solutions floating  
around, some taking advantage of Flash (as I assume the swfupload does).  
Some others I’ve heard of make use of a Java applet. But, I would  
imagine this behavior could be built with JavaScript/AJAX.

In any case I think the “brains” of this feature will need to be  
implemented client-side. The AJAX should be able to send requests to a  
server-side action responsible to accepting one file, and the  
client-side code would make one request per file, plus additional  
request to manipulate the DOM.

Please note this is all speculation. I have no proof-of-concept code to  
offer.

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 8, 2009, 3:59pm UTC](https://www.ruby-forum.com/t/alternative-to-render-update/164890/7 "2009-04-08T15:59:09Z")

</div>

Thanks everyone for the replies.

For now I’m just going with good 'ol standard HTML response until I  
figure out what I’m going to do.
