Rails jQuery UJS回调没有触发

ant*_*tor 6 ajax jquery ujs ruby-on-rails-3

我正在尝试触发ajax:远程链接的成功回调.我在Rails 3.0.8和jquery-rails 1.0.11上.我运行了jquery生成器来安装javascripts.jquery和jquery_uj都包含在我的默认值中,我可以验证它们是否包含在页面中.

这是我的javascript:

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

链接:

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

激活动作:

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
Run Code Online (Sandbox Code Playgroud)

ajax调用似乎工作正常.触发操作,我可以看到数据库中的更改.但是,没有触发ajax回调.我不能让他们中的任何一个工作.我可以获得其他事件,例如点击,以便完美地工作.

如果我在Firebug中调试rails_ujs,我会看到以下代码从Rails ajax块运行:

success: function(data, status, xhr) {
  element.trigger('ajax:success', [data, status, xhr]);
}
Run Code Online (Sandbox Code Playgroud)

我很难过.有任何想法吗?

小智 12

确保在rails.js之后你没有再次包含jquery.js ...我遇到了类似的问题,其中jquery.js被包含两次,并且rails.js包含在其中.