4

Hey I have seen several posts on this, but I am still having issues with calling :method => 'delete' and getting directed to the show method of my controller. The destroy method is working as expect, in that it deletes the comment, but after the request is done, it throws a 404 on GET. Here is the code:

<%= link_to 'delete', "/events/#{@event.id}/comments/#{comment.id}.js", 
        :confirm => 'Are you sure?', 
        :method => :delete, 
        :remote => true %>

Here is the controller method:

def destroy 
  @comment = @event.comments.find(params[:id])
  @comment.destroy
  redirect_to do |format|
    format.html # redirect_to @event, :notice => "comment deleted" }
    format.js { render 'destroy.js.erb' }
  end
end

I've hear that this could be due to not using button_to, but I have tried using button_to as opposed to link_to but this does the same thing.

I've also heard that this could be do to some problems with the way you are using jquery in your set up, but I feel like I doubt that, but here is how I call in jquery just in case (application.html.erb):

<%= javascript_include_tag 'jquery-1.5.2.min.js', 'rails', 'application' %>
  <%= javascript_include_tag 'jquery-ui-1.8.17.custom.min.js' %>

When i watch rails server output I see that it's says:

Redirected to http://0.0.0.0:3000/events/1/comments/35
Completed 302 Found in 132ms
ACTION NAME application


Started GET "/events/1/comments/35" for 127.0.0.1 at Wed Feb 08 16:31:43 -0800 2012

AbstractController::ActionNotFound (The action 'show' could not be found for CommentsController):

Thanks for the help!

botbot
  • 7,299
  • 14
  • 58
  • 96
  • $("#<%= dom_id(@comment) %>").remove() but i commented this out and tested, still the same problem. – botbot Feb 09 '12 at 11:24
  • the answer is that you can't use redirect_to when making an ajax delete request, it will then issue another request and end up in a 404 since the resource is gone. – botbot Feb 13 '12 at 02:46

4 Answers4

1

Rails 3.1 and greater cleans a lot of this up for us...rails.js is gone. Jquery_ujs is part of the gem, but the file should NOT be in your javascript directory: specifically, my jquery_ujs file was old and still "living" in my assets/javascript directory. It still had .live methods which are deprecated. Delete the file!!
Keep the gem 'jquery-rails', '~> 2.1' # REF: https://github.com/rails/jquery-ujs Keep the //= require jquery and //= require jquery_ujs and //= require_tree . In your application.js file Make sure you update the gem and bundle install to get the latest gem. If a javascript error is thrown as was the "sneaky" case for me, the rest of link_to js will not work. Now the delete and the confirmation dialog are working fine. An old jquery_ujs was the culprit.

Tom Bartel
  • 56
  • 5
0

format.html might cause it. Change this line with format.html { render :nothing => true }

mystdeim
  • 4,802
  • 11
  • 49
  • 77
0

I would make sure you have the proper version of rails.js in your app. rails.js is what now takes care of making sure delete links actually use the delete method, so you may need to update your rails.js file. What version of rails are you running?

siannopollo
  • 1,464
  • 11
  • 24
  • Rails 3.1.0. I replace the rails.js with the latest one. Since my app was actually a Rails 3.0 app that I pulled into a new Rails 3.1.0 environment, I figured the rails.js might be not the latest. Still getting the same results. The delete is being made but the redirect to the new method in the controller is still being made. – botbot Feb 10 '12 at 01:07
  • If the delete is going through then the JS isn't the problem. Try taking out the `format.html` line and see if that helps. Or at least put the `format.js` line first. – siannopollo Feb 10 '12 at 06:56
  • hey, siannopollo, i appreciate the suggestion. i commented out the format.html line but it's still trying to get something. i think i'm missing something simple, i believe that it's redirecting to the show method for some reason. i'll keep digging. – botbot Feb 11 '12 at 09:02
0

It might be an issue with your config/routes.rb file. If an earlier entry in the routes.rb file matches events/:id/comments/:id and points to comments#show, it could cause this error.