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!
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
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!
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.
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.
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