Rails & Devise: user accounts are not deleted.

I am using Devise gem to authenticate users for the first time. I can register, log out, log in and edit user accounts without problems, but deleting accounts does not work! When I click the delete button, a JS confirmation appears asking if I want to delete, click "Yes" ... and then redirected to the user's show page. The user account is not corrupted.

In views/devise/registrations/edit.html.erb:

<p>Unhappy? <%= button_to "Cancel my account", '/users/#{@user.id}',   data: { confirm: "Are you sure?" }, method: :destroy, class: "btn btn-danger" %></p>

In my UserController:

  def destroy
    @user = User.find(params[:id])
    @user.destroy

    if @user.destroy
        redirect_to root_path
    end
 end

I am still reeling about Rails routing, but on my .rb routes I have:

devise_for :users, :controllers => { registrations: 'registrations' }

as well as:

resources :users

And when I start the rake routes, there are two routes that I think can be connected:

DELETE /users(.:format)          registrations#destroy
DELETE /users/:id(.:format)      users#destroy

, - , , . !

+4
2
#config/routes.rb
resources :users, only: :destroy

#view
<%= button_to "Cancel my account", @user, method: :delete, data: { confirm: "Are you sure?" } %>

, delete HTTP-; destroy (method: :destroy).

, HTTP , "" (URL). :

HTTP ( ), , . , , , , .

, resources , , :

enter image description here

, , resource, Rails auto URL-/. , URL-, .

+3

<p>Unhappy? <%= button_to "Cancel my account", '/users/#{@user.id}',   data: { confirm: "Are you sure?" }, method: :destroy, class: "btn btn-danger" %></p>

<p>Unhappy? <%= button_to "Cancel my account", '/users/#{@user.id}',   data: { confirm: "Are you sure?" }, method: :delete, class: "btn btn-danger" %></p>

method: :delete not method: :destroy

+1

Source: https://habr.com/ru/post/1613000/


All Articles