自定义收集路径的polymorphic_path

发布于 2024-11-09 18:46:59 字数 984 浏览 9 评论 0原文

我有以下路由定义:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2024-11-16 18:46:59

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 in edit_polymorphic_path to whatever method you want.

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

嘿看小鸭子会跑 2024-11-16 18:46:59

这样就可以了,而且读起来也不错。

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

或者这些

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

并且带有查询参数

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

并且,在视图中您也可以这样做:

= link_to "Documents", [[:filter, @user, :documents], :q => 'keyword']

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']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文