# Problem updating mulitple divs with ajax

**URL:** <https://www.ruby-forum.com/t/problem-updating-mulitple-divs-with-ajax/80378>\
**Category:** Rails\
**Created:** [March 13, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/problem-updating-mulitple-divs-with-ajax/80378 "2006-03-13T16:06:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 13, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/problem-updating-mulitple-divs-with-ajax/80378/1 "2006-03-13T16:06:54Z")

</div>

i’m having some issues updating multiple divs via ajax. perhaps someone  
can  
lend some assistance.

i’ve got 2 divs:

user\_list and user\_form.

when a person clicks the “edit this user” or “add new user” link the  
user\_form div is updated via an ajax call and the form is shown in the  
user\_form div.

this form is a remote form. it submits to either the update or create  
action in the admin/users controller. i’ve verified that this  
submission  
works and the data in the database gets updated.

\<%= form\_remote\_tag :url =\> { :action =\> @action }, :complete =\> “eval(  
request.responseText)” %\>  
…  
\<%= end\_form\_tag %\>

# create or update user then render

@users = User.find(:all)  
@list = render\_to\_string :partial =\> “/admin/users/user\_list”, :layout  
=\>  
false  
logger.info @list  
render :template =\> “/admin/users/update”, :layout =\> false

update.rhtml template

$(‘user\_form’).innerHTML = “”;  
$(‘user\_list’).innerHTML = “\<%= @list -%\>”;  
alert(‘Done’);

the above does not work, specifically setting the user\_list innerHTML to  
the  
value of @list. if i change it to an empty string, the javascript is  
evaluated, both divs innerHTML are set to blank strings and the alert is  
displayed.

I have a feeling it has to do with quotes within the html. however, i  
have  
not figured out a way to escape the quotes correctly so that the is  
displayed. I get no errors in firefox js debugger.

any help would be greatly appreciated.

Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 13, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/problem-updating-mulitple-divs-with-ajax/80378/2 "2006-03-13T23:52:36Z")

</div>

Chris H. wrote:

> update.rhtml template
> 
> $(‘user\_form’).innerHTML = “”;  
> $(‘user\_list’).innerHTML = “\<%= @list -%\>”;  
> alert(‘Done’);

Sounds like a perfect candidate for RJS templates:

page.replace\_html ‘user\_form’, :partial =\> ‘form’ # or ‘’ instead of teh  
partial.  
page.replace\_html ‘user\_list’, :partial =\> ‘list’  
page.alert ‘Done’

You can either get the plugin for 1.0 or update to EdgeRails.

[http://www.codyfauser.com/pages/rjs-plugin](http://www.codyfauser.com/pages/rjs-plugin)

-matthew

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 14, 2006, 11:59am UTC](https://www.ruby-forum.com/t/problem-updating-mulitple-divs-with-ajax/80378/3 "2006-03-14T11:59:41Z")

</div>

Hi Matthew ,  
I’ve posted the problem I have with RJS. Can you give me your opinion ?

My Post\>\>\>\>  
I’m trying to work with RJS and folowed Cody F.'s tutorial  
([http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates](http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates))  
I’ve started by creating a new rails applications and executing  
rake freeze\_edge  
rake rails:update:javascripts  
rake rails:update:scripts

After that I’ve created a controller:  
class DtestController \< ApplicationController  
def add  
end  
end

Created the files add.rhtml and add.rjs in views/dtest/ ,and  
dtest.rhtml in views/layouts.  
I’ve started my server and called my add action and nothing happends .  
Can anyone explain me what I did wrong ?  
My prototype version is 1.5.0\_pre0

Thanx

Matthew I. wrote:

> Chris H. wrote:
> 
> > update.rhtml template
> > 
> > $(‘user\_form’).innerHTML = “”;  
> > $(‘user\_list’).innerHTML = “\<%= @list -%\>”;  
> > alert(‘Done’);
> 
> Sounds like a perfect candidate for RJS templates:
> 
> page.replace\_html ‘user\_form’, :partial =\> ‘form’ # or ‘’ instead of teh  
> partial.  
> page.replace\_html ‘user\_list’, :partial =\> ‘list’  
> page.alert ‘Done’
> 
> You can either get the plugin for 1.0 or update to EdgeRails.
> 
> [http://www.codyfauser.com/pages/rjs-plugin](http://www.codyfauser.com/pages/rjs-plugin)
> 
> -matthew

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 14, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/problem-updating-mulitple-divs-with-ajax/80378/4 "2006-03-14T17:17:24Z")

</div>

> My Post\>\>\>\>  
> I’m trying to work with RJS and folowed Cody F.'s tutorial  
> ([http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates](http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates))  
> I’ve started by creating a new rails applications and executing  
> rake freeze\_edge  
> rake rails:update:javascripts  
> rake rails:update:scripts

First of all, I would have just used the plugin to test in my current  
application. But I guess this would work too.

> After that I’ve created a controller:  
> class DtestController \< ApplicationController  
> def add  
> end  
> end
> 
> Created the files add.rhtml and add.rjs in views/dtest/ ,and  
> dtest.rhtml in views/layouts.

One or the other (add.rhtml or add.rjs), not both. Unless, of course,  
the Rails code detects an AJAX request and serves the appropriate file.  
But I’m not aware of Rails doing this.

An even simpler way to test would be to use the inline version of rjs.  
In your controller for the add() action, put something like this:

render :update do |page|  
page.alert “Success!”  
page.replace\_html “some-div”, “Success!”  
end

So far I haven’t had anything more complex than 2 or three lines so it  
doesn’t make sense to create a whole new RJS file unless it is being  
shared by multiple actions.

You need to make sure you actually have a div with id=“some-div”.

How exactly are you calling the add action, btw? link\_to\_remote?

> I’ve started my server and called my add action and nothing happends .  
> Can anyone explain me what I did wrong ?  
> My prototype version is 1.5.0\_pre0

You mean _nothing_ happens? Nothing in the devel log?

-matthew
