Rails polymorphic link for nested index action

社会主义新天地 提交于 2019-12-14 03:46:33

问题


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.



来源:https://stackoverflow.com/questions/20624266/rails-polymorphic-link-for-nested-index-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!