Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 982
Undistraction Avatar asked Jun 22 '12 13:06

Undistraction


People also ask

How can you tell Rails to render without a layout *?

By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.

What is render partial Rails?

Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.

What is the difference between redirect and render in Ruby on Rails?

There is an important difference between render and redirect_to: render will tell Rails what view it should use (with the same parameters you may have already sent) but redirect_to sends a new request to the browser.

What does render JSON do in Rails?

render :json essentially calls to_json and returns the result to the browser with the correct headers. This is useful for AJAX calls in JavaScript where you want to return JavaScript objects to use. Additionally, you can use the callback option to specify the name of the callback you would like to call via JSONP.


1 Answers

The method to access the format is:

controller.request.format 
like image 168
Anil Avatar answered Oct 09 '22 04:10

Anil