Eric Park
Eric Park

Reputation: 502

Why am I getting a 500 error due to Jquery?

I am getting this error when trying to delete an application listing in my code.

    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

I am trying to implement an UJS delete button. Although the listing deletes, it does not apply the .hide('slow') method in destroy.js.erb file:

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

Destroy Resource in 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 %>

Upvotes: 0

Views: 328

Answers (1)

kd12
kd12

Reputation: 1351

Can you please check by modifying the destroy action in controller? eg.

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

Upvotes: 1

Related Questions