Rails jQuery UJS 回调未触发

发布于 2024-11-16 16:27:20 字数 1124 浏览 5 评论 0原文

我正在尝试触发远程链接的 ajax:success 回调。我使用的是 Rails 3.0.8 和 jquery-rails 1.0.11。我运行 jquery 生成器来安装 javascript。 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]);
}

我被难住了。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菊凝晚露 2024-11-23 16:27:20

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文