Rails 3 AJAX remote form call backs

I'm upgrading from rails 2.3.8 to 3.0.0, so I need to replace the remote_form_for helper calls with form_for(@object, :remote=>true).

I've been following along with Simone Carletti but I cant seem to get the ajax callbacks from rails.js to fire.

My generated HTML is:

<form accept-charset="UTF-8" action="/vendor_shipments" class="new_vendor_shipment" data-remote="true" id="formname" method="post">

The javascript I'm testing with:

jQuery(function($){ 
   alert('document ready');
   $("#formname")
      .bind('ajax:loading', function() {alert("loading!");})
      .bind('ajax:success', function(data, status, xhr) {alert("success!");})
      .bind('ajax:failure', function(xhr, status, error) {alert("failure!");})
      .bind('ajax:complete', function() {alert("complete!");});
});

The 'document ready' alert fires, and the ajax request is successfully executed (data is posted to the server), but none of the 'ajax:____' callbacks fire.

What am I doing wrong?

(for what it's worth, the form itself is loaded via ajax)

Answers


Wow, that was a colossal waste of an evening.

Doing it the way I did, the default prototype rails.js won't linkup the callbacks.

After converting to the jQuery based rails.js (http://github.com/rails/jquery-ujs) callbacks are working fine.


You can trigger the Prototype-based callbacks like so:

$('formname').observe('ajax:before', function(){
  ...
});

$('formname').observe('ajax:complete', function(request){
  ...
});

However, I don't think the request object in the ajax:complete callback contains the response. I'm used to jQuery and had hoped that accessing the response would be simple. Instead, it looks like switching to jQuery-Rails may still be easier, as I can't seem to find drop-in Prototype code to replace the :update functionality from Rails 2 remote helpers.


If the accepted answer don't fixed your problem, check if you have embedded multiple jQuery libraries. In my case I had two different version of jQuery embedded and after removing one of them, everything worked fine.


Need Your Help

Distributed Transaction support for NServiceBus with ActiveMQ?

nservicebus nms

We are looking at using ActiveMQ as the message transport with NServiceBus, instead of MSMQ, to enable Java endpoints to directly integrate with .NET endpoints over ActiveMQ.

Monad escaping inside a StateT context

haskell state-monad aeson

I am trying to get back a value that is in a json feed (via Aeson) directly inside a StateT stacked on IO: