已使用的路线名称无效:'user'

时间:2016-08-08 00:25:27

标签: ruby-on-rails ruby

我正在尝试将追随者添加到我的rails应用程序中。当我运行rails s启动服务器时,我看到以下错误:

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 

路线

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

知道我添加了以下内容可能会有所帮助:

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

在我添加上述内容之前,我的rails应用程序已经运行。

我确定这可能是一个简单的修复,但我是新手。我一直在修补路线和在线搜索超过一个小时,试图理解并解决问题。我希望有更多知识渊博的人能指导我朝着正确的方向前进。

3 个答案:

答案 0 :(得分:3)

由于您正在使用设计,因此devise_for users已创建了一些路由,因此请避免使用与已定义路由冲突的路由。而不是使用as: :user放置另一个相关名称,如as::user_profile

希望有所帮助

答案 1 :(得分:2)

在您的情况下,设计路由和用户路由存在冲突。你需要改变其中一个。您可以将设计置于特定前缀

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

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

答案 2 :(得分:0)

请不要忘记,还有“为” 选项。

devise_for :users, as: 'some_unique_prefix'