oFca
oFca

Reputation: 2830

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).

So, to route to show action, I'd have to have 3 cases:

  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? (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.

Upvotes: 0

Views: 75

Answers (1)

Andy Lindeman
Andy Lindeman

Reputation: 12165

If you're generating a link in a--for instance--link_to, you can use this array syntax:

link_to "Show Discussion", [@parent, @discussion]

Under the hood, this achieves the same thing as the polymorphic_path and polymorphic_url methods:

polymorphic_url([@parent, @discussion])

Upvotes: 1

Related Questions