自定义收集路径的polymorphic_path
我有以下路由定义:
resources :documents do
collection do
post :filter
end
end
以及以下模型结构:
class Document < ActiveRecord::Base
belongs_to :documentable, :polymorphic => true
end
class User < ActiveRecord::Base
has_many :documents, :as => :documentable
end
和控制器结构:
class DocumentsController < ApplicationController
def index
# not important
end
def filter
# not important
end
end
我可以轻松地在视图中说:
polymorphic_path([@user, Document])
获取路径 /users/1/documents,但我希望能够说:
filter_polymorphic_path([@user, Document])
要获取路径/users/1/documents/filter,不幸的是,这不起作用。
任何人都知道我如何能够在不将以下内容添加到我的路线中的情况下完成这个任务,对于我的每个可记录模型:
resources :users do
resources :documents do
collection do
post :filter
end
end
end
I have the following route definition:
resources :documents do
collection do
post :filter
end
end
and the following model structure:
class Document < ActiveRecord::Base
belongs_to :documentable, :polymorphic => true
end
class User < ActiveRecord::Base
has_many :documents, :as => :documentable
end
and controller structure:
class DocumentsController < ApplicationController
def index
# not important
end
def filter
# not important
end
end
I can easily in a view say:
polymorphic_path([@user, Document])
to get the path /users/1/documents, but I want to be able to say:
filter_polymorphic_path([@user, Document])
to get the path /users/1/documents/filter, unfortunately, this doesn't work.
Anyone know how I can pull this off without adding the following to my routes, for each of my documentable models:
resources :users do
resources :documents do
collection do
post :filter
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
polymorphic_path([@user, Document], :action => 'filter')
为您提供/users/:user_id/documents/filter
。另外,
polymorphic_path([@user, Document], :action => 'filter', :sort_order => 'this-order')
为您提供/users/:user_id/documents /filter?sort_order=这个顺序
。我遇到了同样的问题,认为您可以将
edit_polymorphic_path
中的edit
替换为您想要的任何方法。请参阅:http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html
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
inedit_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.
Or these
And with a query param
And, in a view you can also do this: