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

1 view
Skip to first unread message

Javier Quevedo

unread,
Jan 1, 2008, 3:56:39 PM1/1/08
to Ruby on Rails: Talk
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!

Phillip Koebbe

unread,
Jan 1, 2008, 4:11:14 PM1/1/08
to rubyonra...@googlegroups.com

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

Javier Quevedo

unread,
Jan 1, 2008, 4:23:24 PM1/1/08
to Ruby on Rails: Talk
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!

LemmingJoel

unread,
Jan 1, 2008, 5:17:50 PM1/1/08
to Ruby on Rails: Talk
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.
> > Phillip- Hide quoted text -
>
> - Show quoted text -

Javier Quevedo

unread,
Jan 1, 2008, 5:59:26 PM1/1/08
to Ruby on Rails: Talk
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.

LemmingJoel

unread,
Jan 1, 2008, 6:24:41 PM1/1/08
to Ruby on Rails: Talk
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
> > > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages