Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails make all routes default to format: :json

Can you make all routes default to json?

I have the following for an api scope but am wondering if you can do the same for the global scope?

  scope :api, defaults: {format: :json} do
    get "/search(/:query)(/:location)" => "search#index"
  end

For example all user resources would also default to json

resources :users
like image 263
user2954587 Avatar asked Jul 14 '15 13:07

user2954587


2 Answers

Use constraints

constraints format: :json do
  resources :users
end

or

resources :users, :defaults => { :format => 'json' }
like image 185
Pavan Avatar answered Oct 07 '22 07:10

Pavan


According to Rails docs, in your config/routes.rb file:

defaults format: :json do
  resources :photos
end
like image 32
Aleksandrus Avatar answered Oct 07 '22 05:10

Aleksandrus