[英]Show action in polymorphic associations
I have a polymorphic resource Discussion that can belong to either the Project, Task, or Subtask.我有一个多态资源讨论,可以属于项目、任务或子任务。
At one point I wish to reroute to show action of discussions controller.有一次,我希望重新路由以显示讨论控制器的操作。 To do that, I need discussion id (which I get from params) and I need to know what the parent is (which I get from params also).
为此,我需要讨论 id(我从 params 中获得)并且我需要知道父级是什么(我也从 params 中获得)。
So, to route to show action, I'd have to have 3 cases:因此,要路由以显示操作,我必须有 3 种情况:
project_discusison_path(@project, @discussion)
task_discussion_path(@task, @discussion)
subtask_discussion_path(@subtask, @discussion)
How to write this 3 cases in one path helper?如何在一个路径助手中编写这 3 个案例? (looking something like below)
(看起来像下面)
parent_discussion_path(@parent, @discussion)
Remember, I can find and have all the variables.请记住,我可以找到并拥有所有变量。 Only writing the path is the problem.
只有写路径是问题。
If you're generating a link in a--for instance-- link_to
, you can use this array syntax:如果您在 a--for
link_to
生成链接,则可以使用以下数组语法:
link_to "Show Discussion", [@parent, @discussion]
Under the hood, this achieves the same thing as the polymorphic_path
and polymorphic_url
methods:在幕后,这实现了与
polymorphic_path
和polymorphic_url
方法相同的事情:
polymorphic_url([@parent, @discussion])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.