Devise conflicting with other routes using delete

506 views Asked by At

I have these 2 routes that are conflicting in my application

destroy_users DELETE /users/:id(.:format)                  users#destroy
destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy

the corresponding part of my routes file is as follows

match '/users/:id', to: 'users#destroy', :via => :delete, :as =>:destroy_users
devise_for :users
resources :users

When I try to destroy a session using the following link

<li><%= link_to "Sign out", destroy_user_session_path, method: "delete" %></li>

it tries to navigate to the following route localhost:3000/users/sign_out

this is going to my UserController and trying to run the destroy method passing sign_out as a parameter rather than a route

Couldn't find User with id=sign_out

The first line in my routes file was to allow a single user to be deleted in the UserController, this is the destroy method. This had to be put before the devise_for otherwise it was trying to route to the edit_user_registration_path of devise using DELETE. Now it seems to be overwriting the devise destroy_user_session_path

I'm not sure how to fix this any advice would be appreciated

1

There are 1 answers

1
reed_de_la_mer On BEST ANSWER

Typically, I would think devise_for :users creates the following routes

                  Prefix Verb   URI Pattern                    Controller#Action
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy

using your routes you get

                  Prefix Verb   URI Pattern                    Controller#Action
           destroy_users DELETE /users/:id(.:format)           users#destroy <--- oops this should be below all the other routes
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                   users GET    /users(.:format)               users#index
                         POST   /users(.:format)               users#create
                new_user GET    /users/new(.:format)           users#new
               edit_user GET    /users/:id/edit(.:format)      users#edit
                    user GET    /users/:id(.:format)           users#show
                         PATCH  /users/:id(.:format)           users#update
                         PUT    /users/:id(.:format)           users#update
                         DELETE /users/:id(.:format)           users#destroy <--- notice it typically would be

Deleting the first match route should help you... or you can move it below your resources :users. You should skip creating destroy with resources.