2015-07-10 66 views
0

我在嘗試刪除我的代碼中的應用程序列表時遇到此錯誤。爲什麼我會因爲Jquery而發生500錯誤?

DELETE http://localhost:3000/apps/9 500 (Internal Server Error) 
jQuery.ajaxTransport.send @ jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea56bfa5f96ea7c074.js?body=1:9660 
jQuery.extend.ajax @ jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea56bfa5f96ea7c074.js?body=1:9211 
$.rails.rails.ajax @ jquery_ujs.self-ca5248a2fad13d6bd58ea121318d642f195f0b2dd818b30615f785ff365e8d1f.js?body=1:84 
$.rails.rails.handleRemote @ jquery_ujs.self-ca5248a2fad13d6bd58ea121318d642f195f0b2dd818b30615f785ff365e8d1f.js?body=1:162 
(anonymous function) @ jquery_ujs.self-ca5248a2fad13d6bd58ea121318d642f195f0b2dd818b30615f785ff365e8d1f.js?body=1:384 
jQuery.event.dispatch @ jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea56bfa5f96ea7c074.js?body=1:4666 
jQuery.event.add.elemData.handle @ jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea5 

我想實現一個UJS刪除按鈕。雖然上市刪除,它不適用.hide( '慢')方法destroy.js.erb文件:

<% if @app.destroyed? %> 
    $('#app-' +<%= @app.id %>).hide('slow'); 
<% else %> 
    $('#app-' +<%= @app.id %>).prepend("<div class='alert alert-danger'><%= flash[:error] %></div>"); 
<% end %> 

破壞資源的Apps_controller:

def destroy 
    @app = App.find(params[:id]) 

    if @app.destroy 
     flash[:notice] = "App was removed." 
     redirect_to @app 
    else 
     flash[:error] = "App couldn't be deleted. Try again." 
     redirect_to @app 
    end 

    respond_to do |format| 
     format.html 
     format.js 
    end 
    end 

應用指數

<h1>All Apps</h1> 
<% @apps.each do |app| %> 
    <div class="media"> 
    <div class="media-body"> 
     <h5 id="app-<%=app.id%>" class="media-heading"> 
     <%= link_to app.title, app %> 
     <%= link_to "Delete", app , method: :delete, remote: true %> 
     </h5> 
    </div> 
    </div> 
<% end %> 
+0

您應該檢查Rails服務器日誌,因爲這是500錯誤的來源。 – sevenseacat

+1

但是,如何重新定向到剛剛銷燬的應用程序,以便開始。 – sevenseacat

+0

你有沒有檢查應用程序刪除後是否去destroy.js.erb? – kd12

回答

1

你可以通過修改控制器中的銷燬行爲來檢查嗎? 例如。

def destroy 
    @app = App.find(params[:id]) 
    if @app.destroy 
    flash[:notice] = "App was removed." 
    else 
    flash[:error] = "App couldn't be deleted. Try again."  
    end 
    respond_to do |format|  
     format.js { } 
    end 
end 
相關問題