简体   繁体   English

rails:如何建立到某个动作的多态链接

[英]rails: how can I make a polymorphic link to a certain action

I have a class Document (in a mongodb collection) and persist various sub classes (like Page < Document). 我有一个Document类(在mongodb集合中),并保留了各种子类(如Page <Document)。 Now I do a Document.all()- how do I generate links to the document's edit action pointing to the right controller (PageController for pages) without being verbose about it? 现在,我做一个Document.all()-如何生成指向文档编辑操作的链接,该链接指向正确的控制器(页面的PageController),而无需太冗长呢?

<%= link_to document.title, [:admin, document] %>

points to 'show;, how can I make it point to 'edit' without specifying the controller? 指向“显示”;如何在不指定控制器的情况下使其指向“编辑”?

You can prepend :edit to your resource link: 您可以在:edit添加资源链接:

link_to document.title, [:edit, :admin, document]

It will create the proper link (ie /admin/page/1/edit, /admin/document/2/edit), provided that you have a route defined for each resource. 只要您为每个资源定义了一条路由,它将创建适当的链接(即/ admin / page / 1 / edit,/ admin / document / 2 / edit)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM