Undistraction
Undistraction

Reputation: 43401

How to tell which format a controller has resolved to render

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?

Upvotes: 56

Views: 29266

Answers (2)

localhostdotdev
localhostdotdev

Reputation: 1895

in your controller you can do:

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

Upvotes: 21

Anil
Anil

Reputation: 3919

The method to access the format is:

controller.request.format

Upvotes: 91

Related Questions