-1

For my blog, the delete method doesn't work (edit, update, create... are fine).

I already tried different ways of defining the link, but it all didn't help yet. Now at the moment, my html.erb code looks like the following:

<div class="btn">
<%= link_to "Delete", post_path(@post), :confirm => "Are you sure?", :method => :delete %>
</div>

And the controller like this:

def destroy
    @post.destroy
    redirect_to post_path
end

Rake routes:

                    posts GET    /posts(.:format)                    posts#index
                          POST   /posts(.:format)                    posts#create
                 new_post GET    /posts/new(.:format)                posts#new
                edit_post GET    /posts/:id/edit(.:format)           posts#edit
                     post GET    /posts/:id(.:format)                posts#show
                          PATCH  /posts/:id(.:format)                posts#update
                          PUT    /posts/:id(.:format)                posts#update
                          DELETE /posts/:id(.:format)                posts#destroy

3 Answers3

0

try this query for the link:

<div class="btn">
<%= link_to "Delete", @post, :confirm => "Are you sure?", :method => :delete %>
</div>

I think the problem just lies on the post_path(@post). Hope that this works.

0

If the routes are resourceful and the delete is happening in the DB, the problem is probably that the redirect_to route is not plural.

see: http://guides.rubyonrails.org/routing.html#path-and-url-helpers

Corrected:

def destroy
  @post.destroy
  redirect_to posts_path
end
SoAwesomeMan
  • 3,226
  • 1
  • 22
  • 25
0

Please try adding a mechanism to find the @post since http is stateless @post needs to be assigned. @post = Post.find(params[:id]) part has been added for you to try. Thank you.

def destroy
  @post = Post.find(params[:id])
  @post.destroy
  redirect_to posts_path
end
  • It still says "No route matches" and gives me the following route as suggestion: DELETE /posts/:id(.:format) posts#destroy – Matthias Cordes Aug 05 '16 at 11:03
  • Hi @MatthiasCordes can you please post your `rake routes` output or routes.rb file content. Maybe restful routes are not presented. –  Aug 06 '16 at 13:14
  • Yes I saw it seems routes all in order. I searched the issue further and came across one entry on stackoverflow [@link](http://stackoverflow.com/questions/9945441/link-to-method-delete-not-routing-to-destroy-in-rails-3-with-jquery) and one blog entry [@link](https://www.viget.com/articles/delete-in-rails-without-jquery-and-ujs) if you are using JQuery it can cause some issues with delete method if so pls make sure you add these lines `//= require jquery //= require jquery_ujs //= require_tree . ` into `app/assets/javascripts/application.js` I hope this helps. –  Aug 07 '16 at 15:50
  • Hi @MatthiasCordes thank you for your comment and I am glad to hear that it worked. Kindly consider accepting this or any other solution by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. Happy coding! –  Aug 08 '16 at 12:11