Tadas
Tadas

Reputation: 21

Ruby on rails destroy not working

I can't get destroy method to work. When I click on delete, nothing happens. No confirmation pop up, the post doesn't get deleted. The method in controller:

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

The view:

<%= link_to "Delete post", @post, data: { confirm: "Are you sure?" }, :method => :delete %>

Rendered HTML:

<a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/posts/8">Delete post</a>

application.html.erb file is as ror created it:

<!DOCTYPE html>
<html>
<head>
  <title>Myrubyblog</title>
  <%= stylesheet_link_tag 'application', media: 'all', 'dataturbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<% flash.each do |key, value| %>
<p><%= value %></p>
<% end %>
<%= yield %>

</body>
</html>

Edit and everything else works fine. Any ideas to make this work? Thanks

SOLVED! The problem was in application.html.erb file, javascript_include_tag. When I started this project, I was getting 'Rails ExecJS::ProgramError in Pages#home', so I changed javascript_include_tag 'application' into 'default' (as people said in this thread stackoverflow.com/questions/28421547/…). Now I updated it into 'application' and it works as it supposed to. Thanks, Kevin!

Upvotes: 1

Views: 5852

Answers (1)

Kevin
Kevin

Reputation: 1144

Try this

<%= link_to 'Delete post', @post, method: :delete, data: { confirm: 'Are you sure?' } %>

Upvotes: 1

Related Questions