我在使用Rails 3.2.11在AJAX中銷燬對象時遇到問題。Rails 3 Ajax-JQuery:視圖在銷燬對象時未更新
我跟着這個教程: http://www.tutorialspoint.com/ruby-on-rails/rails-and-ajax.htm
控制器工作正常,但在視圖中,沒有什麼是正確完成。我認爲我的問題是JQuery,但我不確定,那麼我的問題在哪裏?
//views/tournaments/index.html.haml
%table.table.table-hover.table-bordered
%thead
%th Name
%th Place
%th Nb players max
%th
- if can? :update, @tournaments
%th
- if can? :destroy, @tournaments
%th
- @tournaments.each do |tournament|
%tr
%td= tournament.name
%td= tournament.place
%td= tournament.nb_players_max
%td= link_to 'Show', tournament
- if can? :update, @tournaments
%td= link_to 'Edit', edit_tournament_path(tournament)
- if can? :destroy, @tournaments
%td= link_to 'Destroy', tournament, method: :delete, data: {confirm: 'Are you sure?'}, :class => 'delete', :remote => true
我控制器tournaments_controller.rb:
def destroy
@tournament = Tournament.find(params[:id])
@tournament.destroy
respond_to do |format|
format.html { redirect_to tournaments_url }
format.json { head :no_content }
format.js { render :layout => false }
end
end
destroy.js.erb:
console.log("hello");
$('.delete').bind('ajax:success', function(){
console.log("delete!");
$(this).parent('tr').fadeOut(400, function(){
$(this).remove();
});
});
控制檯告訴我,該控制器正常工作(200 OK),Hello是顯示在控制檯,但沒有別的...
發生了什麼事? :-(
編輯:
我做了一個測試,專門創建一個項目來測試本教程,它工作正常,但不是在我的項目上面:(
編輯@DickieBoy: 我試過,但沒有奏效:
$(document).ready(function(){
$('.delete').bind('ajax:success', function() {
console.log("delete!");
$(this).closest('tr').fadeOut(400, function() {
$(this).remove();
});
});
});
你從DOM已準備就緒事件中加入AJAX成功事件? – DickieBoy
是否這樣? $(document).bind('ajax:success','。delete',function(){....} –
我會將它作爲答案發布,太難以閱讀。 – DickieBoy