GVS
GVS

Reputation: 229

Invalid route name, already in use: 'user'

I am trying to add followers to my rails application. I am seeing the following error when I run rails s to start my server:

Invalid route name, already in use: 'user'  (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: 
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created 

Routes

Rails.application.routes.draw do
  devise_for :users
  resources :users do
    member do
      get :following, :followers
    end
  end
  resources :relationships, only: [:create, :destroy]
  resources :posts do
    member do
      post '/like' => 'posts#like'
    end
  end
  get ':username' => 'users#show', as: 'user'

  root 'home#index'

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

It may be helpful to know that I added:

  resources :users do
    member do
      get :following, :followers
    end
  end
  resources :relationships, only: [:create, :destroy]

Before I added the above, my rails app worked.


I'm sure it's probably a simple fix, but I am new to rails. I've been tinkering with routes and searching online for over an hour, trying to understand and solve the problem. I'm hoping someone more knowledgeable can guide me in the right direction.

Upvotes: 0

Views: 1701

Answers (3)

Alexandr
Alexandr

Reputation: 1714

Please do not forget, that there is "as" option too.

devise_for :users, as: 'some_unique_prefix'

Upvotes: 0

Hasmukh Rathod
Hasmukh Rathod

Reputation: 1118

In your case, Devise routes and Users routes are conflicting. You need to change one of them. You can either put Devise under a specific prefix like,

devise_for :users, :path_prefix => 'auth'
resources :users

Reference: https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface

Upvotes: 3

Pradeep Sapkota
Pradeep Sapkota

Reputation: 2072

Since you are using devise there are some routes already created by devise_for users so avoid using such routes which has conflict with already defined routes. Instead of using as: :user put another relevant name like as::user_profile

Hope it helps

Upvotes: 4

Related Questions