Render JSON instead of HTML as default?

trnc picture trnc · May 21, 2012 · Viewed 25.8k times · Source

I try to tell rails 3.2 that it should render JSON by default, and kick HTML completely like this:

respond_to :json    

def index
  @clients = Client.all
  respond_with @clients
end

With this syntax, I have to add .json to the URL. How can I achieve it?

Answer

rogeliog picture rogeliog · May 21, 2012

You can modify your routes.rb files to specify the default format

routes.rb

resources :clients, defaults: {format: :json}

This will modify the default response format for your entire clients_controller