我正在尝试触发远程链接的 ajax:success 回调。我在 Rails 3.0.8 和 jquery-rails 1.0.11 上。我运行了 jquery 生成器来安装 javascripts。jquery 和 jquery_ujs 都包含在我的默认值中,我可以验证它们是否包含在页面中。
这是我的javascript:
jQuery(function($) {
$("#deactivate")
.bind("ajax:success", function() {
alert("HELLO");
})
.bind("ajax:error", function() {
alert("GOODBYE");
}
);
});
链接:
= link_to 'Deactivate', activate_patient_path(patient), :id => 'deactivate', :class => "button button-red", :remote => true
激活动作:
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
ajax 调用似乎工作正常。该操作被触发,我可以看到数据库中的更改。但是,没有触发 ajax 回调。我不能让他们中的任何一个工作。我可以让其他事件(例如点击)完美地工作。
如果我在 Firebug 中调试 rails_ujs,我会看到以下代码从 Rails ajax 块运行:
success: function(data, status, xhr) {
element.trigger('ajax:success', [data, status, xhr]);
}
我难住了。有任何想法吗?