Rails 3,为每个资源创建一个新路由

Jer*_* B. 1 routes ruby-on-rails-3

在我正在研究的项目中,我想为多个资源添加相同的路径.我知道我能做到这一点

resources :one do
  collection do
    post 'common_action'
  end
end
resources :two do
  collection do
    post 'common_action'
  end
end  
Run Code Online (Sandbox Code Playgroud)

我至少有10个不同的资源都需要相同的路由,因为每个控制器都有相同的动作.有没有办法更少重复定义?

小智 6

更好的方式和支持轨道3.2

require 'action_dispatch/routing/mapper'
module ActionDispatch::Routing::Mapper::Resources
  alias_method :resources_without_search, :resources

  def resources(*args, &block)
    resources_without_search *args do
      collection do
        match :search, action: "index"
      end
      yield if block_given?
    end
  end
end
Run Code Online (Sandbox Code Playgroud)