8

In my grid this,

link_to('Edit', edit_manage_user_path(user.id))

works fine but this,

link_to('Delete', delete_manage_user_path(user.id))

gives the error "undefined method `delete_manage_user_path' for #<#:0xc05439c>" given that there is a delete action in my controller..

Any idea why this error is coming?

Anss
  • 664
  • 2
  • 7
  • 23
  • You can add `method: :delete` to the `link_to`. Together with that, you can add a confirm step since the deletion isn't reverted, hence the code looks like `link_to 'Delete', manage_user_path(user), method: :delete, data: { confirm: "Are you sure to delete?" }` . – duykhoa Jun 26 '20 at 09:00

6 Answers6

15

Before Rails 7

<%= link_to 'Delete', manage_user_path(user),
  method: :delete, data: {confirm: "Are you sure?"} %>

Rails 7 (with Turbo, out of the box)

<%= link_to 'Delete', manage_user_path(user),
  data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
installero
  • 9,096
  • 3
  • 39
  • 43
9

If you use resources routes, path to destroy action is the same as to show, but you should use HTTP DELETE method:

link_to 'Delete', [:manage, user], method: :delete
Frank Wilson
  • 3,192
  • 1
  • 20
  • 29
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
3
 link_to 'Delete', manage_user_path(user), method: :delete

This will call your delete action. there is no such path delete_manage_user_path(user) if you are using restful routing.

Dipak Gupta
  • 7,321
  • 1
  • 20
  • 32
  • this worked for me, but it was looking for a destroy method in the controller, and after change def delete to def destroy, its working perfectly in Rails 5.2. – tomb Dec 03 '18 at 02:16
2

I always prefered the button way:

 = form_with url: destroy_user_session_path, method: :delete do |form|
   = form.submit "Sign out"
aarkerio
  • 2,183
  • 2
  • 20
  • 34
  • This form button way works for me, while the link constructed with `method: :delete` in the above example never hits the `:destroy` action. Why is that? – nimmolo Jul 07 '22 at 18:23
  • 1
    The "link" version requires some JS rails libraries. Many people remove them, thinking they are not necessary. – aarkerio Jul 07 '22 at 18:47
  • 1
    Ahaa... thank you, yes i found the libraries. It requires both `jquery` and `jquery_ujs` to make these links work. – nimmolo Jul 07 '22 at 20:54
1

Knowing is half the battle. You'll need to figure out which is the correct route for deleting your resource. You can do so by running the following command:

bundle exec rake routes

Here's an example of the output from one of my own Rails apps: enter image description here

I added an arrow to what you should be looking for.

Once you have the route, you'll then need to specify how you want to send the HTTP request when the user clicks on the link. You can do this by supplying the method key in nested the custom data attributes. Here's an example:

<%= link_to "Delete", user_path(user), data: { method: :delete } %>

Additional Resources:

Chris Ledet
  • 11,458
  • 7
  • 39
  • 47
0

The format for the delete call is:

<%= link_to 'Delete', manage_user_path(user.id), :method => :delete %>

use rake routes to learn about the available routes, including generated route helpers, and the controller/action handling the request.

Gagan Gami
  • 10,121
  • 1
  • 29
  • 55