How to tell which format a controller has resolved to render

Ruby on-RailsRuby on-Rails-3ControllerRespond To

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

The method to access the format is:

controller.request.format

Solution 2 - Ruby on-Rails

in your controller you can do:

request.format
request.format.html?
request.format.js?
request.format.json?
# etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUndistractionView Question on Stackoverflow
Solution 1 - Ruby on-RailsAnilView Answer on Stackoverflow
Solution 2 - Ruby on-RailslocalhostdotdevView Answer on Stackoverflow