没有路由匹配错误 - 使用嵌套资源

时间:2011-05-23 15:48:05

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

我的routes.rb文件中有以下嵌套资源

  resource :issuer do
    resources :certificates
  end

我的模特:

class Issuer < ActiveRecord::Base
  has_many :certificates
end

class Certificate < ActiveRecord::Base
  belongs_to :issuer
end

访问时出现以下错误: /issuer/2/certificates

No route matches {:action=>"edit", :controller=>"certificates"}

任何帮助将不胜感激..提前致谢!

编辑:我的rake routes

    issuer_certificates GET    /issuer/:issuer_id/certificates(.:format)          {:action=>"index", :controller=>"certificates"}
                        POST   /issuer/:issuer_id/certificates(.:format)          {:action=>"create", :controller=>"certificates"}
 new_issuer_certificate GET    /issuer/:issuer_id/certificates/new(.:format)      {:action=>"new", :controller=>"certificates"}
edit_issuer_certificate GET    /issuer/:issuer_id/certificates/:id/edit(.:format) {:action=>"edit", :controller=>"certificates"}
     issuer_certificate GET    /issuer/:issuer_id/certificates/:id(.:format)      {:action=>"show", :controller=>"certificates"}
                        PUT    /issuer/:issuer_id/certificates/:id(.:format)      {:action=>"update", :controller=>"certificates"}
                        DELETE /issuer/:issuer_id/certificates/:id(.:format)      {:action=>"destroy", :controller=>"certificates"}

3 个答案:

答案 0 :(得分:2)

您应该使用资源(复数)而不是资源来参考:issuer。所以请试试这个:

resources :issuers do
   resources :certificates
end

issuers/2/certificates将有效。

答案 1 :(得分:1)

在routes.rb文件中,您将发布者定义为单一资源

  resource :issuer do         # <-----  using resource instead of resources
    resources :certificates
  end 

使用单一资源,您获得的路由将不允许:id匹配,因此在您的示例中,

issuer/2/certificates

这是给你悲伤的“2”。你应该摆脱它,或者定义:issuer作为routes.rb文件中的标准(非单数)资源。

答案 2 :(得分:0)

您没有为PUT定义/issuer/2/certificates。相反,你已经为/issuer/2/certificates/123定义了它。这就是你得到这个错误的原因。

如果您要修改certificate,请确保生成正确的链接并添加certificate_id