加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 自定义收集路由的polymorphic_path

发布时间:2020-12-16 21:29:53 所属栏目:百科 来源:网络整理
导读:我有以下路线定义: resources :documents do collection do post :filter endend 以及以下模型结构: class Document ActiveRecord::Base belongs_to :documentable,:polymorphic = trueendclass User ActiveRecord::Base has_many :documents,:as = :docum
我有以下路线定义:
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

解决方法

polymorphic_path([@ user,Document],:action =>’filter’)为您提供/ users /:user_id / documents / filter.

此外,polymorphic_path([@ user,:action =>’filter’,:sort_order =>’this-order’)为您提供/ users /:user_id / documents / filter?sort_order = this-order.

我遇到了同样的问题,认为你可以将edit_polymorphic_path中的编辑替换为你想要的任何方法.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读