Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails render head vs. status

Tags:

What's the difference between render head :ok vs. render status :ok in Rails? They both get returned as the header right?

like image 202
stackjlei Avatar asked Oct 28 '17 23:10

stackjlei


People also ask

What does head do in Rails?

In Rails, the head method is shorthand for "respond only with this status, headers, and an empty body." The head method takes a symbol corresponding to a status, in this case :no_content for "204."

What is head OK?

The head method can be used to send responses with only headers to the browser. The head method accepts a number or symbol (see reference table) representing an HTTP status code. head :ok sets render to return just the header with status 200. It's merely a shorthand for render nothing: true, status: :ok .

How can you tell Rails to render without a layout?

2.2. By default, if you use the :plain 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 and use the . text. erb extension for the layout file.

What does render do in Rails?

Rendering is the ultimate goal of your Ruby on Rails application. You render a view, usually . html. erb files, which contain a mix of HMTL & Ruby code.


1 Answers

There is no difference really. The Rails doc says this about head:

The head method can be used to send responses with only headers to the browser. The head method accepts a number or symbol (see reference table) representing an HTTP status code

head :ok sets render to return just the header with status 200.

It's merely a shorthand for render nothing: true, status: :ok.

Rails 5 will also do head :no_content by default when you don't have a template defined for an action

like image 62
Cyzanfar Avatar answered Sep 20 '22 13:09

Cyzanfar