尝试删除代码中的应用程序列表时出现此错误。
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删除按钮。虽然列表删除,但它不会在destroy.js.erb文件中应用.hide('slow')方法:
<% 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
App Index
<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 :(得分: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