6

我正在尝试触发远程链接的 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]);
}

我难住了。有任何想法吗?

4

1 回答 1

12

确保在 rails.js 之后不再包含 jquery.js ...我遇到了类似的问题,其中 jquery.js 包含两次,rails.js 包含在两者之间。

于 2011-06-29T09:59:18.623 回答