自定义收集路线的polymorphic_path [英] polymorphic_path for custom collection route

查看:97
本文介绍了自定义收集路线的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 ,但是我想说:

to get the path /users/1/documents, but I want to be able to say:

filter_polymorphic_path([@user, Document])

要获取路径/users/1/documents/filter ,很遗憾,这不起作用.

to get the path /users/1/documents/filter, unfortunately, this doesn't work.

对于每个 documentable 模型,每个人都知道如何在不添加以下内容的情况下实现这一目标:

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

推荐答案

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=this-order.

我遇到了同样的问题,认为您可以将edit_polymorphic_path中的edit替换为所需的任何方法.

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

请参阅: http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html

这篇关于自定义收集路线的polymorphic_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆