Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force render format for all actions

I want to respond with json to all formats.

I can force the render format to json so the action will render show.json despite the accept header:

  def show
    render formats: :json
  end

How I can set render format for all actions of the controller?

Something like this:

class GalleriesController < ApplicationController
  formats :json
end
like image 806
freemanoid Avatar asked Nov 07 '13 10:11

freemanoid


2 Answers

As a summary of all comments to the questions and readability for future users, you can do, as mentioned here:

before_filter :default_format_json

def default_format_json
  request.format = "json"
end
like image 68
Dirty Henry Avatar answered Nov 17 '22 09:11

Dirty Henry


In your controller:

def my_action
  formats.clear
  formats << :json
end

(I've only tested this in Rails 4.2 and 3.2.)

formats returns an Array of format symbols. It is delegated to @_lookup_context, which is an instance of ActionView::LookupContext.

like image 1
JellicleCat Avatar answered Nov 17 '22 10:11

JellicleCat