Search code examples
ruby-on-railsjsonrespond-to

How to globally PREVENT json rendering in rails?


I have a rails application in which most of the actions respond to json.

Is there any "switch" I can turn off to prevent all the controllers from responding to json despite the respond_to method call, or I still have to disable it manually in every action (which seems very odd).


Solution

  • I have a proposal, though a bit hacky I'm afraid :)

    class < ApplicationController
      before_filter :disable_json
    
      def disable_json
        if request.format =~ /json/
          //do something you like, redirect_to or reply with message
        end
      end
    

    The before_filter will be fired before any specific controller's method.

    The json header is usually "application/json"

    For request, you can read more here: http://guides.rubyonrails.org/action_controller_overview.html#the-request-object