Search code examples
ruby-on-railsrubyrails-routing

How to set default format for routing in Rails?


There is the following code for routing:

  resources :orders, only: [:create], defaults: { format: 'json' }
  resources :users,  only: [:create, :update], defaults: { format: 'json' } 
  resources :delivery_types, only: [:index], defaults: { format: 'json' }
  resources :time_corrections, only: [:index], defaults: { format: 'json' }

It is possible to set default format for all resources using 1 string without 'defaults' hash on each line? Thanks.


Solution

  • Try something like this:

    scope format: true, defaults: { format: 'json' } do
      resources :orders, only: [:create]
      resources :users,  only: [:create, :update] 
      resources :delivery_types, only: [:index]
      resources :time_corrections, only: [:index]
    end