GVS GVS - 4 months ago 9
Ruby Question

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.

Answer

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