ruby-on-railsrubyblogsdelete-method

Ruby delete method does not work


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

Solution

  • 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