Rails polymorphic link for nested index action

2019-06-21 19:37发布

问题:

I have been trying to find this one link for hours now. I have a polymorphic association where both collections & assortments have designs.

Collection model

has_many :designs, :as => :targetable

Assortment model

has_many :designs, :as => :targetable

Design model

belongs_to :targetable, :polymorphic => true


In order to link to the design's 'show' action, the proper polymorphic path would be:

link_to polymorphic_path([@targetable, @design])

But I can't figure out how to link to the design's 'index' page to show all the designs associated with it's respective targetable object.

Does anyone know the appropriate link to get there??

回答1:

I was finally able to find the answer.

polymorphic_path([@targetable, Design])

Instead of using a variable as the second object in the polymorphic path, you are supposed to use the model name. This path links to

targetable/:targetable_id/designs

Maybe this will help someone for future use.