Tamer Shlash Tamer Shlash - 1 year ago 89
JSON Question

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

method call, or I still have to disable it manually in every action (which seems very odd).

Answer Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download