2011-06-20 101 views
6

我試圖爲遠程鏈接觸發ajax:success回調。我在Rails 3.0.8和jQuery-rails 1.0.11上。我運行jquery生成器來安裝javascripts。 jquery和jquery_ujs都包含在我的默認值中,我可以驗證它們是否包含在頁面中。Rails 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 

Activate動作:

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]); 
} 

我難倒。有任何想法嗎?

+1

如果我執行 element = $(「#activate」); element.trigger('ajax:success'); 在控制檯我可以得到回調觸發。出於某種原因,它只是不適用於UJS。 – anthonator

+0

使用Firebug,如果我在rails_ujs中的element.trigger('ajax:success',[data,status,xhr])行放置斷點,然後將$(「#deactivate」)綁定到ajax:停止回調時成功會觸發。當我點擊鏈接直到刷新頁面時,回調將繼續正確觸發。看起來$(「#deactivate」)與觸發器正在執行的元素不是相同的引用。 – anthonator

回答

12

請確保您沒有在rails.js之後再次包括jquery.js ...我遇到了一個類似的問題,其中jquery.js被包含兩次,並且包含rails.js。

+1

就是這樣。 jQuery Tools嵌入了jQuery的舊版本。 – anthonator