gpt4 book ai didi

ruby-on-rails - 有时在 Rails 中出现 "Invalid Route Name"错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:09 25 4
gpt4 key购买 nike

只有当我尝试在本地主机中加载我的 Rails 应用程序时,才会偶尔收到以下错误。

Invalid route name, already in use: 'root' 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
get 'welcome/index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"

Rails.application.routes do
resources :articles
root 'welcome#index' #-->This is the line that causes the error
end

resources :articles do
resources :comments
end
end

我真的是 Rails 的新手,我不确定是什么导致了这个问题,或者如果我在网络上实际托管这个初学者项目是否会成为一个问题。另外,如果有帮助,我正在运行 Ruby 2.2.2 和 Rails 4.2.1。

预先感谢您的帮助!

最佳答案

您将 (Rails.application.routes) 嵌套在 (Rails.application.routes.draw) 中。运行 rake routes ,你会看到你有两次文章的资源。此外,你有 root 'welcome#index' 嵌套在里面,这就是你收到错误的原因。你的路线应该是这样的

Rails.application.routes.draw do
get 'welcome/index' => 'welcome#index'
root 'welcome#index'
resources :articles do
resources :comments
end
end

请注意,您的应用程序的路由含义 (/) 和 (/welcome/index) 都指向欢迎 Controller 索引操作。您实际上并不需要 get 'welcome/index' => 'welcome#index' 并且您可以删除该行并在需要来自欢迎 Controller 的索引操作时使用 root。

关于ruby-on-rails - 有时在 Rails 中出现 "Invalid Route Name"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30282652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com