【问题标题】:`add_route': Invalid route name, already in use: 'new_user_session' (ArgumentError)`add_route':无效的路由名称,已在使用:'new_user_session'(ArgumentError)
【发布时间】:2016-04-28 06:25:27
【问题描述】:

我无法通过 Heroku 让我的应用在生产环境中运行。它是一个正在开发中的完全可用的应用程序。

我在 SO 上研究了这个问题,但许多解决方案是因为他们的 routes.rb 中有重复的 devise_for。我的应用程序没有这个问题,而且我很难找出这种重复发生的位置。

这是完整的错误信息:

/app/vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:557:in `add_route': 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: 
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created

这是我当前的routes.rb 文件:

Rails.application.routes.draw do

devise_for :users, :controllers => { registrations: 'registrations' }

get 'items/create'

get  'welcome/index'

get  'about' => 'welcome#about'

get  'brandon' => 'welcome#brandon'

root 'welcome#index'

resources :users, only: [:index, :show] do
  resources :items
 end

end

我已经更新了我的 gem,删除了数据库并重新迁移,但没有任何效果。

【问题讨论】:

  • 可能不相关,但您的前两个 GET 路由未显示要定向到哪个控制器和操作,也请尝试将您的根放在 devise_for 路由下方。

标签: ruby-on-rails ruby heroku routes precompile


【解决方案1】:
#config/routes.rb
Rails.application.routes.draw do

  resources :items, only: [:new, :create], path_names: { new: "create" }    
  resources :welcome, path: "", only: :index do #-> url.com/
     collection do
        get :about   #-> url.com/about
        get :brandon #-> url.com/brandon
     end
  end

  resources :users, only: [:index, :show] do #-> there may be a conflict with "/users/" as devise also uses "/users/" path
     resources :items #-> url.com/users/:user_id/items
  end

  devise_for :users, controllers: { registrations: 'registrations' }

  root "welcome#index"
end

如果它不起作用,我会删除它;您要么对所做的“裸”声明有问题(始终尝试在 resources 附近确定您的路由范围),要么您的生产环境中有一个冲突的文件。

【讨论】:

  • 嗨 Rich - 感谢您的评论!我尝试使用您设置的路线,但我再次遇到同样的错误。不过,我非常感谢您关于设置路线的通知——“赤裸裸的声明”只是我开始学习如何编写路线的方式。这种方式肯定更清晰:)
【解决方案2】:

我也遇到过类似的情况。 devise_for 的 as 选项的使用为我解决了。

devise_for :users, as: "unique_prefix", :controllers => { registrations: 'registrations' }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    相关资源
    最近更新 更多