# Is there any way to update elements of the webpage with multiple threads in an action?

**URL:** <https://www.ruby-forum.com/t/is-there-any-way-to-update-elements-of-the-webpage-with-multiple-threads-in-an-action/124727>\
**Category:** Rails\
**Created:** [January 1, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-update-elements-of-the-webpage-with-multiple-threads-in-an-action/124727 "2008-01-01T21:57:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Javier\_Q](https://www.ruby-forum.com/u/Javier_Q)\
**Post date:** [January 1, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-update-elements-of-the-webpage-with-multiple-threads-in-an-action/124727/1 "2008-01-01T21:57:03Z")

</div>

I’m trying to update some elements of the webpage with AJAX, RJS, etc  
by making threads in an action of a controller and trying to partial  
render the results inside each thread.  
The problem is that rails wont let me have more than 1 render or  
redirect per action.

Is there any possible way to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![Javier\_Q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Javier\_Q](https://www.ruby-forum.com/u/Javier_Q)\
**Post date:** [January 1, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-update-elements-of-the-webpage-with-multiple-threads-in-an-action/124727/2 "2008-01-01T22:11:41Z")

</div>

On Jan 1, 2008, at 2:56 PM, Javier Q. wrote:

> I’m trying to update some elements of the webpage with AJAX, RJS, etc  
> by making threads in an action of a controller and trying to partial  
> render the results inside each thread.  
> The problem is that rails wont let me have more than 1 render or  
> redirect per action.
> 
> Is there any possible way to do this?

This may be enough to get you started.

You can use render :update (though some people will shun you for  
doing so) and call replace\_html multiple times. Something like

render :update do |page|  
page.replace\_html ‘some\_div’, :partial =\> ‘some\_partial’  
page.replace\_html ‘some\_other\_div’, :partial =\> ‘some\_other\_partial’  
page \<\< “some raw javascript if you want”  
end

There are many things you can do with render :update.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![Javier\_Q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Javier\_Q](https://www.ruby-forum.com/u/Javier_Q)\
**Post date:** [January 1, 2008, 10:24pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-update-elements-of-the-webpage-with-multiple-threads-in-an-action/124727/3 "2008-01-01T22:24:34Z")

</div>

I was actually doing that at the begining but id like to do that  
render partial in different momtents. Each thread does something and  
then they update a certain div in the page. This is more less the code

def parse\_services

```
lfmthread=Thread.new do
      parse_service_1

    lfmthread.exit
  end

  ytthread=Thread.new do

      parse_service_2
    ytthread.exit
  end

```

end

def parse\_service\_1  
…generate data…  
render :update do |page|  
page.replace\_html :service\_1\_results, :partial =\>  
‘tag’, :object =\> @tag  
end  
end

Is it possible to do something like this?  
Thanks again!

---

<div class="post-metadata">

**Author:** ![Javier\_Q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Javier\_Q](https://www.ruby-forum.com/u/Javier_Q)\
**Post date:** [January 1, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-update-elements-of-the-webpage-with-multiple-threads-in-an-action/124727/4 "2008-01-01T23:18:31Z")

</div>

Perhaps rather than calling render from each individual thread, have  
each thread send their results to some intermediary object. That  
object would then call render only once when it had received all the  
data from each thread.

I hope this is relevant. I’m not 100% sure I’m on the same page as  
you.

---

<div class="post-metadata">

**Author:** ![Javier\_Q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Javier\_Q](https://www.ruby-forum.com/u/Javier_Q)\
**Post date:** [January 2, 2008, 12:00am UTC](https://www.ruby-forum.com/t/is-there-any-way-to-update-elements-of-the-webpage-with-multiple-threads-in-an-action/124727/5 "2008-01-02T00:00:06Z")

</div>

I definitly think that we are on the same page. Its just that an  
intermediary object doesn’t help since the method of the object is  
called from the action itself and we have the same problem.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![Javier\_Q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Javier\_Q](https://www.ruby-forum.com/u/Javier_Q)\
**Post date:** [January 2, 2008, 12:25am UTC](https://www.ruby-forum.com/t/is-there-any-way-to-update-elements-of-the-webpage-with-multiple-threads-in-an-action/124727/6 "2008-01-02T00:25:15Z")

</div>

This is what I mean:

class Renderer

def initialize no\_of\_threads  
@max = no\_of\_threads  
@count = 0  
@content\_to\_render = []  
end

def addContent dom\_id, content  
@content\_to\_render \<\< {:dom\_id =\> dom\_id, :content =\> content}  
@count++  
if @count == @max  
output  
end  
end

def output  
render :update do |page|  
@content\_to\_render.each do |content|  
page.replace\_html content[:dom\_id], content[:content]  
end  
end  
end

end

renderer = Renderer.new(2)

thread1=Thread.new do  
renderer.addContent :some\_dom\_id, (render\_to\_string :partial =\>  
“name\_of\_partial”)  
end

thread2=Thread.new do  
renderer.addContent :another\_dom\_id, “some more generated content”  
end

render\_to\_string thing not tested - I saw it at  
[http://snippets.dzone.com/posts/show/396](http://snippets.dzone.com/posts/show/396)
