路由资源不处理“新”路由资源正如预期的那样

发布于 2024-12-17 14:39:04 字数 1652 浏览 2 评论 0原文

我有一个简单的 Rails 应用程序,我试图在其中向数据库添加一个非常简单的记录类型(“client_types”)。

我在 routes.rb 中有一条路线,内容如下:

资源:client_types

据我了解,它应该是我的 client_types 资源的所有常规路由的代理。

因此,当我浏览到以下 URL http://localhost:3000/client_types/new 时,我在运行时收到以下路由错误:

没有路由匹配 {:action=>"show ", :controller=>"client_types"}

请注意,这里所涉及的操作是显示操作,而不是新操作(我在控制器中有针对这两个操作的方法)。

所以...我在上面的资源路由下面添加了以下路由,并且中提琴,它有效:

match 'client_types/new' => 'client_types#new', :as => :client_type

那么我错过了什么?我的假设是,我的路由文件中的 resources :client_types 会添加一条与我后来明确添加的路由相匹配的路由。

rake paths揭示了以下内容:

client_types     GET    /client_types(.:format)          {:action=>"index", :controller=>"client_types"}
                 POST   /client_types(.:format)          {:action=>"create", :controller=>"client_types"}
new_client_type  GET    /client_types/new(.:format)      {:action=>"new", :controller=>"client_types"}
edit_client_type GET    /client_types/:id/edit(.:format) {:action=>"edit", :controller=>"client_types"}
client_type      GET    /client_types/:id(.:format)      {:action=>"show", :controller=>"client_types"}
                 PUT    /client_types/:id(.:format)      {:action=>"update", :controller=>"client_types"}
                 DELETE /client_types/:id(.:format)      {:action=>"destroy", :controller=>"client_types"}
 client_type            /client_types/new(.:format)      {:controller=>"client_types", :action=>"new"}

I have a simple rails application in which I'm trying to add a very simple type of record ("client_types") to a database.

I have a route in routes.rb which reads:

resources :client_types

And as I understand it, should be a proxy to all of the conventional routes for my client_types resource.

So when I browse to the following URL http://localhost:3000/client_types/new, I get the following routing error at runtime:

No route matches {:action=>"show", :controller=>"client_types"}

Notice the action in question here is show, not new (and I have a method for both of these in my controller).

So... I added the following route below the resources route above, and viola, it works:

match 'client_types/new' => 'client_types#new', :as => :client_type

So what am I missing? My assumption was that resources :client_types in my routing file would have added a route matching the one I explicitly added later.

rake routes reveals the following:

client_types     GET    /client_types(.:format)          {:action=>"index", :controller=>"client_types"}
                 POST   /client_types(.:format)          {:action=>"create", :controller=>"client_types"}
new_client_type  GET    /client_types/new(.:format)      {:action=>"new", :controller=>"client_types"}
edit_client_type GET    /client_types/:id/edit(.:format) {:action=>"edit", :controller=>"client_types"}
client_type      GET    /client_types/:id(.:format)      {:action=>"show", :controller=>"client_types"}
                 PUT    /client_types/:id(.:format)      {:action=>"update", :controller=>"client_types"}
                 DELETE /client_types/:id(.:format)      {:action=>"destroy", :controller=>"client_types"}
 client_type            /client_types/new(.:format)      {:controller=>"client_types", :action=>"new"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2024-12-24 14:39:04

现在这正在发挥作用。我的一个观点有问题,这个错误消息是一个转移注意力的信息。

This is now working. I had an issue in one of my views and this error message was a red herring.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文