Problem updating mulitple divs with ajax


#1

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


#2

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

-matthew


#3

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)
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

-matthew


#4

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)
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