Forum: Ruby on Rails Complex Javascipt Stopped Working after AJAX Call

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Alat M. (Guest)
on 2007-01-01 22:30
This has to be the weirdest problem ever.
I will be as specific as possible.

Let's say I have a partial "_profile.rhtml".
It is first displayed in "user.rhtml" using:

<div id="ajaxWrapper">
  <%= render :partial=>'profile' %>

All javascripts on the "_profile" partial works (more specifically, a
lightbox script (ibox) and a sortable table script). However, when the
profile is updated via an AJAX call, say via someMethod in my user

render :update do |page|
  page.replace_html 'ajaxContent', :partial => "profile"

The partial is read and rendered in the the correct div and everything
seems working except that all the "complex" javascripts stopped working.
(i.e. Table no longer sorts and lightbox doesn't appear) I've added a
line of "simple" javascript to "_profile.rhtml":

<a href="javascript:alert('hi')">simple</a>

And that worked both before and after the AJAX update.

What may be the issue here? Thanks.
Alat M. (Guest)
on 2007-01-01 23:53
Alat M. (Guest)
on 2007-01-02 02:31
Patiently waiting for the gurus :)
Phlip (Guest)
on 2007-01-02 02:42
(Received via mailing list)
Alat Meth wrote:

> Patiently waiting for the gurus :)

That's how this shop works.

While you wait, you could run in Firefox with Firebug installed. The FAQ
Ajax stopped working" has the first answer "A browser will often decline
run any Javascript after an error". So trap the error and you might just
solve it yourself.

If you do, please remember to finish your own thread, so the archives
have a false trail!

  Phlip <-- NOT a blog!!!
Alat M. (Guest)
on 2007-01-02 02:53
Thanks Phlip, I've followed your advice but firebug has detected no JS
errors. Meanwhile, I've also commented out most of the stuff that is not
relavant but it seems that javascript just refuse to function after been
called via AJAX. (The AJAX update itself is working fine)

Any other possibilities? Could it be a configuration problem?
Phlip (Guest)
on 2007-01-02 02:58
(Received via mailing list)
> Any other possibilities? Could it be a configuration problem?

Try the Rails-spinoffs mailing list (stupid name, I know) for the
interaction between Rails and the JS libraries.

  Phlip <-- NOT a blog!!!
Alat M. (Guest)
on 2007-01-02 13:42
Still waiting for guru :)
brian.estlin (Guest)
on 2007-01-02 18:10
(Received via mailing list)
I'm no guru, but I had a similar problem once so maybe my experience
will be helpful.

This was a while back, and I'm barely awake yet, so forgive my

In my situation, the JS in the partial was setting up some structures,
lets say a sortable list, but there was a single line of javascript at
the end of the page which "turned on" all the objects. When reloading a
partial, all the setup code got re-done, but the code outside the
partial was of course not re-loaded, so the objects remained inert.

Alat M. (Guest)
on 2007-01-02 19:33
Hey thanks Brian. Any other suggestions?
Alat M. (Guest)
on 2007-01-02 20:09
Alat Meth wrote:
> Hey thanks Brian. Any other suggestions?

Thanks Brian, it worked like a charm.

For future reference, most JS libraries call to init itself by:
Event.observe(window, 'load', SortableTable.load);

But after an AJAX call, you might have to reload it manually, add this
to the link_to_remote or observe_field etc.
:complete => "SortableTable.load()"

This topic is locked and can not be replied to.