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

时间:2017-03-09 13:08:43

标签: ruby-on-rails rails-routing

假设我有以下条件适用于大量资源:

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

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

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

1 个答案:

答案 0 :(得分:1)

routes.rb只是红宝石,所以你可以把红宝石代码放在这个顶部

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

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