[Ruby] Invalid route name, already in use: Error resolution for’new_user_session’

1 minute read

Environment

Mac OS Catalina Ruby 2.7.1 Rails 6.0.3.2

Phenomenon

Introduce devise for login function and set route. After that, I ran Rails routes and 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 be saying.

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 didn’t define it in two places”, but I suddenly realized that the line was added at the top w Resolved by removing the devise_for :users line.

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 boot safely!

Finally

It seems to be good to think that the same routing is already set for the error in already in use system like this time. (It is as it is) When the same error occurs in the future, it seems to be able to be resolved quickly.

Tags: ,

Updated: