> For the complete documentation index, see [llms.txt](https://kelby.gitbook.io/rails-beginner-s-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kelby.gitbook.io/rails-beginner-s-guide/actioncontroller/outside_the_metal/actioncontroller_head.md).

# Head

**返回一个内容为空的 response.**

有时候(作为 Web service 时)，响应内容可能只需要一个状态码，其它内容都不需要。

直接用 `head` 方法:

```ruby
head :created, :location => person_url(@person)
```

注意：使用 head 只是设置了 respond\_body，如果程序还没有结束，那么 Controller#action 层面的后续代码还是会执行的。

结果和使用 render nothing: true 类似，所以你也可以这么做：

```ruby
headers['Location'] = person_url(@person)
render :nothing => true, :status => "201 Created"
```

当然，如果有多个地方使用到此功能的话，render 写起来还是挺让人头疼的。

**附：对照表**

| Response Class | HTTP Status Code                     | Symbol                  |
| -------------- | ------------------------------------ | ----------------------- |
| 消息             | 100                                  | :continue               |
| 101            | :switching\_protocols                |                         |
| 102            | :processing                          |                         |
| 成功             | 200                                  | :o                      |
| 201            | :created                             |                         |
| 202            | :accepted                            |                         |
| 203            | :non\_authoritative\_information     |                         |
| 204            | :no\_content                         |                         |
| 205            | :reset\_content                      |                         |
| 206            | :partial\_content                    |                         |
| 207            | :multi\_status                       |                         |
| 208            | :already\_reported                   |                         |
| 226            | :im\_used                            |                         |
| 重定向            | 300                                  | :multiple\_choice       |
| 301            | :moved\_permanently                  |                         |
| 302            | :found                               |                         |
| 303            | :see\_other                          |                         |
| 304            | :not\_modified                       |                         |
| 305            | :use\_proxy                          |                         |
| 306            | :reserved                            |                         |
| 307            | :temporary\_redirect                 |                         |
| 308            | :permanent\_redirect                 |                         |
| 客户端错误          | 400                                  | :bad\_reques            |
| 401            | :unauthorized                        |                         |
| 402            | :payment\_required                   |                         |
| 403            | :forbidden                           |                         |
| 404            | :not\_found                          |                         |
| 405            | :method\_not\_allowed                |                         |
| 406            | :not\_acceptable                     |                         |
| 407            | :proxy\_authentication\_required     |                         |
| 408            | :request\_timeout                    |                         |
| 409            | :conflict                            |                         |
| 410            | :gone                                |                         |
| 411            | :length\_required                    |                         |
| 412            | :precondition\_failed                |                         |
| 413            | :request\_entity\_too\_large         |                         |
| 414            | :request\_uri\_too\_long             |                         |
| 415            | :unsupported\_media\_type            |                         |
| 416            | :requested\_range\_not\_satisfiable  |                         |
| 417            | :expectation\_failed                 |                         |
| 422            | :unprocessable\_entity               |                         |
| 423            | :locked                              |                         |
| 424            | :failed\_dependency                  |                         |
| 426            | :upgrade\_required                   |                         |
| 428            | :precondition\_required              |                         |
| 429            | :too\_many\_requests                 |                         |
| 431            | :request\_header\_fields\_too\_large |                         |
| 服务端错误          | 500                                  | :internal\_server\_erro |
| 501            | :not\_implemented                    |                         |
| 502            | :bad\_gateway                        |                         |
| 503            | :service\_unavailable                |                         |
| 504            | :gateway\_timeout                    |                         |
| 505            | :http\_version\_not\_supported       |                         |
| 506            | :variant\_also\_negotiates           |                         |
| 507            | :insufficient\_storage               |                         |
| 508            | :loop\_detected                      |                         |
| 510            | :not\_extended                       |                         |
| 511            | :network\_authentication\_required   |                         |
