Reputation: 107
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
Upvotes: -1
Views: 687
Reputation:
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
Upvotes: 0
Reputation: 3416
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
Upvotes: 0
Reputation: 191
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.
Upvotes: 0