1

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!

Tadas
  • 21
  • 1
  • 4
  • Yes, do you have `'jquery-rails'` gem installed? – Uri Agassi Mar 06 '16 at 14:50
  • See if this helps you: http://stackoverflow.com/a/15800485/1120015 – Uri Agassi Mar 06 '16 at 14:51
  • You can see if there are any JavaScript errors in your console. – Martin Tournoij Mar 06 '16 at 14:54
  • It gives this js error in console: "ActionController::RoutingError (No route matches [GET] "/javascripts/default.js")" but I haven't used any js in this project yet. – Tadas Mar 06 '16 at 15:12
  • I think the problem is in the javascript_include_tag in your application.html.erb file. Can you edit your post including the application file? – Kevin Etore Mar 06 '16 at 15:25
  • I finally got it working. You were right, Kevin, it was 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 http://stackoverflow.com/questions/28421547/rails-execjsprogramerror-in-pageshome). I updated it into 'application' and it works as it supposed to. Thank you, guys! – Tadas Mar 06 '16 at 15:37
  • No problem, glad we could be of help! – Kevin Etore Mar 06 '16 at 15:40

1 Answers1

1

Try this

<%= link_to 'Delete post', @post, method: :delete, data: { confirm: 'Are you sure?' } %>
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
Kevin Etore
  • 1,046
  • 3
  • 14
  • 32
  • Make it a instance variable `@post` – Kevin Etore Mar 06 '16 at 14:45
  • It is an instance variable @post. – Tadas Mar 06 '16 at 14:52
  • You mentioned an error: `undefined method post` i can see that in the controller it is a instance variable, but did you make the link_to an instance variable aswell? – Kevin Etore Mar 06 '16 at 14:54
  • Yea, it's also instance variable in the link_to. It gave that error when I tried your code before you edited it, now the error is gone, but the promlem is still here. I don't have any adblock to block confirm pop up. I also tried to get destroy working without confirmation, but it doesn't delete post either. – Tadas Mar 06 '16 at 15:19