6

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.

siekfried
  • 2,964
  • 1
  • 21
  • 36
malcoauri
  • 11,904
  • 28
  • 82
  • 137
  • Isn't the routing of different request types derived from the header request (http://stackoverflow.com/questions/1595424/request-format-returning/1595453#1595453), not in a param? – Richard Peck Jan 09 '14 at 10:29

3 Answers3

9

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
siekfried
  • 2,964
  • 1
  • 21
  • 36
3

I'd rather add method to application_controller. And use it as before filter where I want.

class ApplicationController < ActionController::Base
...
private 
...
  def set_default_format
    params[:format] ||= "json"
  end
end

class UsersController < ApplicationController
  before_filter :set_default_format, only: [:create]
  ...
end

In this case default format wouldn't a surprise for new developers, because usually routes.rb is big and cumbersome

Ivan Denysov
  • 1,502
  • 12
  • 16
0

That worked for me:

  scope defaults: { format: 'json' } do
    resources :users, only: [:index]
  end
Dorian
  • 22,759
  • 8
  • 120
  • 116