Rails 如何能避免在路由中反复引入 concerns

mfb777 · March 08, 2022 · Last by mfb777 replied at March 13, 2022 · 493 hits

比如如下代码

  # config/routes.rb
Rails.application.routes.draw do

  concern :searchable do
    get :search, on: :collection
  end

  namespace :admin do
    resources :posts, concerns: [:searchable]
    resources :comments, concerns: [:searchable]
    resources :tags, concerns: [:searchable]
  end

end

有很多个相同的结构,使用什么办法能不需要每次都写那个 concerns: [:searchable]

也行 search 单独写一个 controller,通过 type 来区分?看样子是 admin,又没有特别的要求的话,可以把 search 功能通过参数集成到 index 里面

Reply to piecehealth

感谢,这个就是我想要的。

You need to Sign in before reply, if you don't have an account, please Sign up first.