Rails 渲染头部与状态

Rails render head vs. status

render head :ok 和 Rails 中的 render status :ok 有什么区别?它们都作为 header 返回,对吗?

真的没有区别。 Rails doc 关于 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 将渲染设置为 return 只是状态为 200 的 header。

它只是 shorthand 的 render nothing: true, status: :ok

Rails 当您没有为操作定义模板时,默认情况下 5 也会执行 head :no_content