Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails jQuery UJS callbacks not triggering

I'm attempting to trigger the ajax:success callback for a remote link. I'm on Rails 3.0.8 and jquery-rails 1.0.11. I ran the jquery generator to install the javascripts. Both jquery and jquery_ujs are included in my defaults and I can verify they are being included on the page.

Here's my javascript:

jQuery(function($) {
  $("#deactivate")
    .bind("ajax:success", function() {
      alert("HELLO");
    })
    .bind("ajax:error", function() {
      alert("GOODBYE");
    }
  );
});

The link:

= link_to 'Deactivate', activate_patient_path(patient), :id => 'deactivate', :class => "button button-red", :remote => true

The activate action:

def activate
    patient = Patient.find(params[:id])
    patient.do_not_call = !patient.do_not_call
    if patient.save
      render :nothing => true
    else
      render :status => 500, :nothing => true
    end
end

The ajax call seems to be working fine. The action is triggered and I can see the changes in the database. However, the ajax callbacks are not being triggered. I can't get any of them to work. I can get other events, such as click, to work perfectly.

If I debug rails_ujs in Firebug I see that the following code gets ran from the Rails ajax block:

success: function(data, status, xhr) {
  element.trigger('ajax:success', [data, status, xhr]);
}

I'm stumped. Any ideas?

like image 390
anthonator Avatar asked Jun 20 '11 01:06

anthonator


1 Answers

make sure that you are not including jquery.js again after rails.js...I ran into a similar problem where jquery.js was included twice, and rails.js was included in between.

like image 158
Eyal Kedem Avatar answered Sep 28 '22 07:09

Eyal Kedem