Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 693
Pravin Avatar asked Nov 24 '10 05:11

Pravin


2 Answers

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
});
like image 138
Fredrik Boström Avatar answered Nov 01 '22 15:11

Fredrik Boström


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.

like image 1
calasyr Avatar answered Nov 01 '22 14:11

calasyr