Stuart Halloway
2006-Mar-22 16:08 UTC
[Rails] Multiple Updates, Multiple Renders, One Ajax Call
The following code snippet allows you to update multiple DOM elements
in a single Ajax call in Rails (1.1):
# pass in a hash of domid=>template name
def update_many(options)
render :update do |page|
options.each do |k,v|
page.replace_html k, render(:partial=>v)
page.visual_effect :highlight, k
end
end
end
So, if you wanted to update the preview div from the content partial,
and the status div from the status partial, you could now say:
update_many ''preview''=>''content'',
''status''=>''status''
Notice that I appear to be breaking the rules, in that I am calling
render multiple times (once for each item in the option hash). I
expected to have to call render_to_string instead, but that blows up,
and this works fine.
Is this behavior by design, or a coincidence? I?d like to get this
documented and make sure that it is considered a feature.
Regards,
Stuart Halloway
www.relevancellc.com
P.S. Is there a totally better way to do this that I missed? :-)
Stuart Halloway
2006-Mar-22 16:50 UTC
[Rails] Multiple Updates, Multiple Renders, One Ajax Call
Hah -- nevermind, nothing like hitting ''send'' to make you see the right answer. I see now that I can use the :partial option directly without calling render at all.> The following code snippet allows you to update multiple DOM > elements in a single Ajax call in Rails (1.1): > > # pass in a hash of domid=>template name > def update_many(options) > render :update do |page| > options.each do |k,v| > page.replace_html k, render(:partial=>v) > page.visual_effect :highlight, k > end > end > end > > So, if you wanted to update the preview div from the content > partial, and the status div from the status partial, you could now > say: > > update_many ''preview''=>''content'', ''status''=>''status'' > > Notice that I appear to be breaking the rules, in that I am calling > render multiple times (once for each item in the option hash). I > expected to have to call render_to_string instead, but that blows > up, and this works fine. > > Is this behavior by design, or a coincidence? I?d like to get this > documented and make sure that it is considered a feature.
Maybe Matching Threads
- rendering a partial inside another using render_to_string and (:formats) in controller
- render_to_string in an after filter
- how do I render a partial view into a string from inside a view ?
- Apache+mod_proxy_balancer+Mongrel+Mephisto, Apache kills CPU
- render_to_string in a controller test?