user2954587
user2954587

Reputation: 4861

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

Upvotes: 9

Views: 13460

Answers (2)

Aleksandrus
Aleksandrus

Reputation: 1754

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

defaults format: :json do
  resources :photos
end

Upvotes: 4

Pavan
Pavan

Reputation: 33542

Use constraints

constraints format: :json do
  resources :users
end

or

resources :users, :defaults => { :format => 'json' }

Upvotes: 15

Related Questions