53

In a rails controller action with the following code:

respond_to do |format|
  format.json{ render :json=>  {:status => 200, :response=>@some_resource} }
  format.html { redirect_to(some_resource_path)}
end

How can I log the format the controller will resolve i.e. 'HTML' or 'json'? format is of type Collector. Is there a way of getting a string denoting the format?

Stefan
  • 96,300
  • 10
  • 122
  • 186
Undistraction
  • 38,727
  • 46
  • 165
  • 296
  • 1
    try params[:format] http://stackoverflow.com/questions/1671111/methods-for-limiting-the-rails-render-format-to-html – gayavat Jun 22 '12 at 14:00

2 Answers2

87

The method to access the format is:

controller.request.format
Anil
  • 3,829
  • 1
  • 17
  • 28
14

in your controller you can do:

request.format
request.format.html?
request.format.js?
request.format.json?
# etc.
localhostdotdev
  • 1,221
  • 7
  • 17
  • I can't find any documentation on the available types or how they are built. Do you know where this is defined? – estani Aug 17 '20 at 12:24