[RUBY] Invalid route name, already in use: Error resolution for'new_user_session'

environment

Mac OS Catalina Ruby 2.7.1 Rails 6.0.3.2

phenomenon

Introduce devise for login function and set route. After that, when I ran Rails routes, I got the following error.

Invalid route name, already in use: 'new_user_session'  (ArgumentError)
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: 
https://guides.rubyonrails.org/routing.html#restricting-the-routes-created

Apparently, the route named'new_user_session'is already in use! It seems to say.

The route when the phenomenon occurs is as follows.

routes.rb


Rails.application.routes.draw do
  devise_for :users
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  
  root to: 'toppages#index'
  
  resources :users, only: [:index, :show, :edit, :update, :destroy] do
    member do
      get :followings
      get :followers
      get :likes
    end
  end
  
  devise_for :users,
    path: '',
    path_names: {
      sign_up: '',
      sign_in: 'login',
      sign_out: 'logout',
      registration: 'signup'
    },
    controllers: {
      registrations: 'users/registrations',
      sessions: 'users/sessions',
      passwords: 'users/passwords'
    }
  devise_scope :user do
    get 'signup', to: 'users/registrations#new'
    get 'login', to: 'users/sessions#new'
    get 'logout', to: 'users/sessions#destroy'
  end

  resources :posts, only: [:create, :destroy] do
    collection do
      get :search
    end
  end
  
  resources :relationships, only: [:create, :destroy]
  resources :favorites, only: [:create, :destroy]
  
end

Solution

I was thinking "I haven't defined two places", but suddenly I noticed that a line was added at the top w Resolved by removing the line devise_for: users.

routes.rb


Rails.application.routes.draw do
  root to: 'toppages#index'
  
  resources :users, only: [:index, :show, :edit, :update, :destroy] do
    member do
      get :followings
      get :followers
      get :likes
    end
  end
  
  devise_for :users,
    path: '',
    path_names: {
      sign_up: '',
      sign_in: 'login',
      sign_out: 'logout',
      registration: 'signup'
    },
    controllers: {
      registrations: 'users/registrations',
      sessions: 'users/sessions',
      passwords: 'users/passwords'
    }
  devise_scope :user do
    get 'signup', to: 'users/registrations#new'
    get 'login', to: 'users/sessions#new'
    get 'logout', to: 'users/sessions#destroy'
  end

  resources :posts, only: [:create, :destroy] do
    collection do
      get :search
    end
  end
  
  resources :relationships, only: [:create, :destroy]
  resources :favorites, only: [:create, :destroy]
  
end

I was able to start it safely!

Finally

For errors that are already in use, it seems good to think that the same routing has already been set as in this case. (As it is) If the same error occurs in the future, it seems to be resolved quickly.

Recommended Posts

Invalid route name, already in use: Error resolution for'new_user_session'
wsimport error handling (A class / interface with the same name "xxx" is already in use)
Address already in use workaround (Windows)
[Cloud9] Address already in use [Solution]
Multiple migrations have the name Error resolution
`bind': Address already in use --bind (2) for 127.0.0.1:3000 (Errno :: EADDRINUSE) How to deal with the error