Ok, here is my specific set up:
I’ve got a form for a model A and inside the form is a partial containg
fields for an associated model B. Initially two sets of fields for B
are loaded with the page, but the user can remove or add more fieldsets
dynamically using button inputs. This is where I’ve implemented UJS:
<% unless author.eql? @authors.first %>
<%= content_tag "button", "Remove this author", :type =>
“submit”, :id => “remove-author-#{index}”, :name => “remove_author”,
:value => index %>
<% apply_behavior “#remove-author-#{index}:click”,
remote_function(:url => {:action => :remove_author, :remove => index},
:update => “revision_authors”, :submit => “revision_authors”),
:prevent_default => true %>
<% end %>
<% if author.eql? @authors.last %>
<%= content_tag “button”, “Add another author”, :type =>
“submit”, :id => “add-author-#{index}”, :name => “add_author” %>
<% apply_behavior “#add-author-#{index}:click”,
remote_function(:url => {:action => :add_author}, :update =>
“revision_authors”, :submit => “revision_authors”), :prevent_default =>
true %>
<% end %>
This works great first time I click either add or remove. The AJAX
kicks in quite niceley and reloads the fields --one set for each
author-- and those two apply_behavior calls. But after the ajax is
complete and a set of fields has either been added or removed, none of
the buttons work with AJAX. Instead they revert to their default
behavior which is to submit the whole form.
Also in case it’s helpful, I inspected the JS for the page before
making the first AJAX request and there was a reference to
/behaviours/action.js, but that file didn’t exist on the server.
I appreciate your attempting to work me through this. Obviously I
don’t fully comprehend how the caching works.
Floyd