what's the path to destroy registration with d

2019-06-16 18:19发布

问题:

I'm trying to add a link so the user can destroy his/her own account. I'm using the built-in registration class.

In my view I have <%= link_to 'Destroy', current_user, :confirm => 'Are you sure you want to destroy your account?', :method => :delete %> pointing to localhost:3000/users/4 by example

First of all, is that the correct link to use? Secondly, how to redirect to root path because presently it looks like it tries to redirect to user with id 4 (and it fails because it is protected).

Rake routes gives DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}

Thanks in advance.

回答1:

Try

<%= link_to 'Destroy', user_registration_path, :confirm => 'Are you sure you want to destroy your account?', :method => :delete %>

It's because of devise treat registration as Singular Resource.

Besides, run rake routes and you can see details about registration routing:

user_registration      POST   /users(.:format)                                       {:action=>"create", :controller=>"devise/registrations"}
new_user_registration  GET    /users/sign_up(.:format)                               {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET    /users/edit(.:format)                                  {:action=>"edit", :controller=>"devise/registrations"}
user_registration      PUT    /users(.:format)                                       {:action=>"update", :controller=>"devise/registrations"}
user_registration      DELETE /users(.:format)                                       {:action=>"destroy", :controller=>"devise/registrations"}

It means:

  • user_registration_path is a helper method that returns /users(.format)
  • Perform DELETE request on /users(.format) will delete the registration