Observe_field loses event binding after firing

I’m running into an odd problem where I’m observing two radio buttons
via observe_field to make AJAX calls, and it seems to lose the event
binding after the first call.

The basic functionality is that clicking on the “teacher” radio button
will show the list of subjects, while clicking on the “student” button
will hide these again.

The portions of the rhtml template:

<%= radio_button_tag “user_type”, “Student”, true %>Student
<%= observe_field “user_type_student”, :url => { :action =>
“ajax_hide_subject_list” }, :method => :get %>
<%= radio_button_tag “user_type”, “Teacher”, false %>Teacher
<%= observe_field “user_type_teacher”, :url => { :action =>
“ajax_show_subject_list” }, :method => :get %>

<ul id="ajax_subject_list" style="display:none">
  <%= render :partial => "subject", :collection => @subjects %>
</ul>

The corresponding controller actions:

def ajax_show_subject_list
render :update do |page|
page.show :ajax_subject_list
end
end

def ajax_hide_subject_list
render :update do |page|
page.hide :ajax_subject_list
end
end

Very simple. The problem I’m seeing is that an AJAX call is fired
only on the first click. Subsequent clicks don’t do anything. It
looks like the binding of the event to the DOM ID is lost somehow
during the server update.

Questions:
What are good debugging tools to find out what events are associated
to a given DOM ID?
Is this a Rails bug? Are there workarounds short of going to full
custom Javascript?

Thanks,

Wolf

No replies here, so we figured out a workaround ourselves. In the reply
from the server, we add a replace call to submit a new observe_field
that hooks to the radio buttons.

I think think there should be an easier way to call some Prototype API
to re-register the existing handler, but this works for now.

Make a partial, _observers.rhtml:

<%= observe_field "user_type_student", :url => { :action => "ajax_hide_subject_list" }, :method => :get %> <%= observe_field "user_type_teacher", :url => { :action => "ajax_show_subject_list" }, :method => :get %>

Be sure this partial is rendered on the template somewhere. This will
hook up the observers when the page is first loaded.

Then, on the ajax updates, re-submit the partial again:

def ajax_show_subject_list
render :update do |page|
page.show :ajax_subject_list
page.replace “observers”, :partial => “observers”
end
end

def ajax_hide_subject_list
render :update do |page|
page.hide :ajax_subject_list
page.replace “observers”, :partial => “observers”
end
end

Oh, just found this bug entry you may be interested in:

http://dev.rubyonrails.org/ticket/7895

Hi,

I have just run in to that problem. The difference is that I am using
a :function instead of a :url for now. Anyway, adding a :frequency
seemed to solve the problem. Now my :function is properly fired every
time a field change is observed.

HTH


Yves-Eric