无路由匹配错误 - 使用嵌套资源
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用资源(复数)而不是引用 :issuer 中的资源。因此,请尝试以下操作:
issuers/2/certificates
将会起作用。You should use resources (plural) instead of resource in refer to :issuer. So please try this:
and
issuers/2/certificates
will work.在你的routes.rb 文件中,你将发行者定义为单一资源。
对于单一资源,你获得的路由将不允许 :id 匹配,因此在你的示例中,
“2”让你感到悲伤。您应该摆脱它或将 :issuer 定义为您的 paths.rb 文件中的标准(非单一)资源。
In your routes.rb file you are defining issuer as a singular resource
With singular resources, the routes you get won't allow an :id to be matched, so in your example,
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.
您没有为
/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 includecertificate_id
.