polymorphic_path for custom collection route

て烟熏妆下的殇ゞ 提交于 2019-12-04 06:31:20

polymorphic_path([@user, Document], :action => 'filter') gives you /users/:user_id/documents/filter.

Also, polymorphic_path([@user, Document], :action => 'filter', :sort_order => 'this-order') gives you /users/:user_id/documents/filter?sort_order=this-order.

I ran into the same problem thinking you can replace the edit in edit_polymorphic_path to whatever method you want.

See: http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html

This would do it, and it reads nicely.

polymorphic_path([:filter, @user, Document])

Or these

polymorphic_path([:filter, @user, :documents])
polymorphic_path([:filter, @user, Document.new])

And with a query param

polymorphic_path([:filter, @user, Document], :q => 'keyword')

And, in a view you can also do this:

= link_to "Documents", [[:filter, @user, :documents], :q => 'keyword']
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!