polymorphic_path для пользовательского маршрута сбора - PullRequest
6 голосов
/ 26 мая 2011

У меня есть следующее определение маршрута:

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

Ответы [ 2 ]

14 голосов
/ 01 июня 2011

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 в edit_polymorphic_path на любой метод, который вы хотите.

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

3 голосов
/ 20 августа 2014

Это будет сделано, и это будет приятно читать.

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']
...