# What is the best way to update more that 1 div in AJAX call

**URL:** <https://www.ruby-forum.com/t/what-is-the-best-way-to-update-more-that-1-div-in-ajax-call/164417>\
**Category:** Rails\
**Created:** [April 1, 2009, 7:19am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-update-more-that-1-div-in-ajax-call/164417 "2009-04-01T07:19:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 1, 2009, 7:19am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-update-more-that-1-div-in-ajax-call/164417/1 "2009-04-01T07:19:41Z")

</div>

Hello,

I want to update more than 1 div inside the page with 1 AJAX call.  
I noticed you can only render 1 at a time in Rails methods and also you  
can only set 1 update: inside form\_remote\_for.

What is the best way to update more than once?

I was thinking to deliver it back in hidden field and then use  
JavaScript to upload it in the other DIV. I’m guessing though there must  
be better way to do this in rails

Any ideas?

Thanks,

Tam

---

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 1, 2009, 7:24am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-update-more-that-1-div-in-ajax-call/164417/2 "2009-04-01T07:24:34Z")

</div>

> I want to update more than 1 div inside the page with 1 AJAX call.  
> I noticed you can only render 1 at a time in Rails methods and also  
> you  
> can only set 1 update: inside form\_remote\_for.
> 
> What is the best way to update more than once?

Look into update\_page and the methods you can put inside it’s block  
(particular replace\_html). In the form\_remote\_for/link\_to\_remote case  
you don’t specify the :update parameter. Let the RJS that gets  
returned do your bidding. You’re still only rendering one thing, but  
that render is a bunch of JS that does what you want…

update\_page(&block)

Yields a JavaScriptGenerator and returns the generated JavaScript  
code. Use this to update multiple elements on a page in an Ajax  
response. See JavaScriptGenerator for more information.

Example:

update\_page do |page|  
page.hide ‘spinner’  
# and other things…  
end

---

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 1, 2009, 7:49am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-update-more-that-1-div-in-ajax-call/164417/3 "2009-04-01T07:49:53Z")

</div>

Hi  
In controller suppose action is replace\_this then create a file  
replace\_this.rjs in the same folder  
Now in action write  
render :action =\> 'replace\_this.rjs"

And open replace\_this.rjs  
page[:div1].replace\_html :partial=\>“partial\_here”}

```
  Above you can use :inline, etc instead of :partial

```

Look  
[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html)

Sijo

---

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 1, 2009, 8:20am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-update-more-that-1-div-in-ajax-call/164417/4 "2009-04-01T08:20:32Z")

</div>

Thanks a lot Philip and Sijo, you solutions are very helpful I will be  
using RJS for that
