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

发布于 2024-11-09 09:14:04 字数 1513 浏览 0 评论 0原文

我的 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

没有路线匹配 {:action=>"edit", :controller=>"certificates"}

如有任何帮助,我们将不胜感激.. 提前致谢!

编辑:我的耙路线

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

I have the following Nested Resource in my routes.rb file

  resource :issuer do
    resources :certificates
  end

My Models:

class Issuer < ActiveRecord::Base
  has_many :certificates
end

class Certificate < ActiveRecord::Base
  belongs_to :issuer
end

I get the following error when i visit: /issuer/2/certificates

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

Any help would be appreciated.. Thanks in advance!

EDIT : MY 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"}

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

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

发布评论

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

评论(3

辞取 2024-11-16 09:14:04

您应该使用资源(复数)而不是引用 :issuer 中的资源。因此,请尝试以下操作:

resources :issuers do
   resources :certificates
end

issuers/2/certificates 将会起作用。

You should use resources (plural) instead of resource in refer to :issuer. So please try this:

resources :issuers do
   resources :certificates
end

and issuers/2/certificates will work.

夏末 2024-11-16 09:14:04

在你的routes.rb 文件中,你将发行者定义为单一资源。

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

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

issuer/2/certificates

“2”让你感到悲伤。您应该摆脱它或将 :issuer 定义为您的 paths.rb 文件中的标准(非单一)资源。

In your routes.rb file you are defining issuer as a singular resource

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

With singular resources, the routes you get won't allow an :id to be matched, so in your example,

issuer/2/certificates

it's the "2" that's giving you grief. You should either get rid of it or define :issuer as a standard (non-singular) resource in your routes.rb file.

花期渐远 2024-11-16 09:14:04

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

如果您尝试编辑证书,请确保生成正确的链接并包含certificate_id

You don't have PUT defined for /issuer/2/certificates. instead you have it defined for /issuer/2/certificates/123. That's why you get that error.

If you are trying to edit the certificate then make sure you generate a correct link and include certificate_id.

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