Pravin
Pravin

Reputation: 6662

Ajax callbacks in rails3

In rails 2 for a ajax form we can have ajax callbacks like before, after etc. How to do it in rails 3.

Upvotes: 8

Views: 5400

Answers (3)

calasyr
calasyr

Reputation: 346

Binding a function to ajax:success as morbaq suggested worked for me.

However, I think if there is more than one ajax behavior associated with the element, the function will run for each of them.

Upvotes: 1

crazycrv
crazycrv

Reputation: 2445

In case you also want to add listener for ajax send use following code

$(document).ready(function() {
  $('#topnav-buildings').bind('ajax:beforeSend', function() {
    blockDiv('#topnav-buildings');
  });
});

I spent an hour to find these t2 things

  1. we have to put it inside document.ready
  2. S is in caps when you write ajax:beforeSend
  3. Dont use ajaxSend instead of ajax:beforeSend .... beacause it adds global handler for all ajax events

Upvotes: 0

Fredrik Boström
Fredrik Boström

Reputation: 1511

I'm having the same problem, and this post helped me figure it out. Basically you need to attach a listener to the element that reacts on rails' callback events. For example:

<%= link_to 'Delete', post, 
  :confirm => 'Are you sure?', 
  :method => :delete, 
  :remote=>true, 
  :id=>'delete' %>

In a separate js file (to make it unobtrusive), with jQuery:

$('#delete').bind('ajax:success', function() {
  // do something
});

Upvotes: 17

Related Questions