如何在 Rails 控制器中返回 HTTP 204 [英] How to return HTTP 204 in a Rails controller

查看:44
本文介绍了如何在 Rails 控制器中返回 HTTP 204的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很基础,但我是 Ruby/Rails 初学者.我只需要在控制器中返回 HTTP 204.会

This seems to be basic but I'm a Ruby/Rails beginner. I need to simply return HTTP 204 in a controller. Would

respond_to do |format|
  format.html  
end

返回 204?

推荐答案

head :no_content

使用 Rails 3.2.x、4.x 测试.它导致控制器方法以 204 No Content HTTP 状态代码响应.

Tested with Rails 3.2.x, 4.x. It causes the controller method to respond with the 204 No Content HTTP status code.

在名为 foobar 的控制器方法中使用它的示例:

An example of using this inside a controller method named foobar:

def foobar
  head :no_content
end

这篇关于如何在 Rails 控制器中返回 HTTP 204的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆