将相同的选项应用于多条路线

apply same options to multiple routes

假设我有以下条件可以申请一堆资源:

:only => [:create, :destroy], :defaults => {:format => :json}, :constraints => {:format => :json}

我的问题是如何将这些条件同时应用于多条路线,例如:

group resources [:resource1, :resource2] do
    :only => [:create, :destroy], :defaults => {:format => :json}, :constraints => {:format => :json}
end

routes.rb 就是 ruby,所以你可以像这样把 ruby 代码放在上面

my_defaults = { 
  only:        [:create, :destroy], 
  defaults:    { format: :json }, 
  constraints: { format: :json }
}

Rails.application.routes.draw do
  # ...
  resources :users, my_defaults
  # ...
end