gpt4 book ai didi

ruby-on-rails - 为多个资源添加公共(public)路由

转载 作者:行者123 更新时间:2023-12-04 05:52:42 25 4
gpt4 key购买 nike

如何添加 /search一口气连接所有这些资源?

MyCoolApp::Application.routes.draw do

resources :posts, only [:index, :show, :create] do
collection { get :search }
end

resources :authors, only [:index, :show] do
collection { get :search }
end

resources :comments, only: [:index, :show] do
collection { get :search }
end

resources :categories, only: [:index, :show] do
collection { get :search }
end

resources :tags, only: [:index] do
collection { get :search }
end
end

我看到了 this answer这很接近,但我需要能够指定到达资源的操作。有没有更好的方法以更好的方式注入(inject)搜索路线?
%w(one two three four etc).each do |r|
resources r do
collection do
post 'common_action'
end
end
end

像这样的东西?
resources :posts, only [:index, :show, :create, :search]

最佳答案

我知道您已标记此问题 ruby-on-rails-3 ,但我将为将来遇到此问题的任何人提供 Rails 4 解决方案。

Rails 4 介绍 Routing Concerns

concern :searchable do
collection do
get :search
end
end

resources :posts, only [:index, :show, :create], concerns: [:searchable]
resources :authors, only [:index, :show], concerns: [:searchable]
resources :comments, only: [:index, :show], concerns: [:searchable]
resources :categories, only: [:index, :show], concerns: [:searchable]
resources :tags, only: [:index], concerns: [:searchable]

关于ruby-on-rails - 为多个资源添加公共(public)路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18664332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com