SIDDHARTH JAIN
SIDDHARTH JAIN

Reputation: 39

ruby on rails 4.2.6 "delete action" not working

I am working on ruby on rails 4.2.6 , this is my show.html file

<h1><%= @post.title %></h1>
<p><%= @post.body %></p>
<p><%= @post.created_at %></p>
<%= link_to "Edit Post",edit_post_path %> | <%=link_to 'Delete Post',@post,method: :delete %>

and This is my code inside posts controller

def destroy
 @post = Post.find(params[:id])
 @post.destroy
 redirect to post_path,:notice =>"your post has been deleted"
 end
end

The date is not deleting and it is showing and the error coming inside command prompt is

action controller::routing error( no routing matches      [get]"/javascripts/default.js)

Kindly Help !

Upvotes: 1

Views: 294

Answers (3)

Asnad Atta
Asnad Atta

Reputation: 4003

in your controller

redirect to post_path

is wrong you are redirecting to show page you will need to redirect on index page.

redirect to posts_path

Upvotes: 0

Larry Lv
Larry Lv

Reputation: 769

1). The error doesn't mean the "delete action" not working, it's saying that somewhere in your html/erb, you want to load '/javascripts/default.js', but there is no definition for this url in your config/routes.rb

2). The destroy controller code you provided has a redundant end, should be like this:

def destroy
  @post = Post.find(params[:id])
  @post.destroy
  redirect to post_path, notice: "your post has been deleted"
end

3). I think your server should log some exceptions for that syntax error, whenever something goes wrong, the log file should be the first few things you check.

Upvotes: 0

Tan Nguyen
Tan Nguyen

Reputation: 3376

The destroy method should be:

def destroy
  @post = Post.find(params[:id])
  @post.destroy
  redirect_to posts_path, :notice => "your post has been deleted"
end

Upvotes: 1

Related Questions