【问题标题】:Routing resource does not handle "new" as expected路由资源未按预期处理“新”
【发布时间】:2011-11-24 12:39:56
【问题描述】:

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

我在routes.rb 有一条路线,上面写着:

resources :client_types

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

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

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

请注意这里有问题的操作是显示,而不是新的(我的控制器中有这两种方法)。

所以...我在上面的资源路线下方添加了以下路线,中提琴,它可以工作:

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

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

rake routes 揭示了以下内容:

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"}

【问题讨论】:

  • rake routes 报告您看到的内容
  • “当我着陆时” - 请描述您如何“着陆”,例如通过网址或链接?
  • “当我登陆时”是指浏览器中的网址。
  • 请发布您的 routes.rb 文件内容

标签: ruby-on-rails ruby ruby-on-rails-3 url-routing rails-routing


【解决方案1】:

这正在工作。我的一个观点有问题,这个错误消息是一个红鲱鱼。

【讨论】:

    猜你喜欢
    • 2012-10-04
    • 2012-10-04
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2014-05-02
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多