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?

1
  • 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?" } . Commented Jun 26, 2020 at 9:00

6 Answers 6

25

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?'} %>
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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.

1 Comment

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.
2

I always prefered the button way:

 = form_with url: destroy_user_session_path, method: :delete do |form|
   = form.submit "Sign out"

3 Comments

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?
The "link" version requires some JS rails libraries. Many people remove them, thinking they are not necessary.
Ahaa... thank you, yes i found the libraries. It requires both jquery and jquery_ujs to make these links work.
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:

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.