路由资源不处理“新”路由资源正如预期的那样
我有一个简单的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在这正在发挥作用。我的一个观点有问题,这个错误消息是一个转移注意力的信息。
This is now working. I had an issue in one of my views and this error message was a red herring.