Ruby-on-rails 如何在 Rails 控制器中返回 HTTP 204

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8592921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:31:18  来源:igfitidea点击:

How to return HTTP 204 in a Rails controller

ruby-on-railsruby

提问by Nonos

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

这似乎是基本的,但我是 Ruby/Rails 初学者。我只需要在控制器中返回 HTTP 204。将

respond_to do |format|
  format.html  
end

return a 204?

返回 204?

回答by Eliot Sykes

head :no_content

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

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

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

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

def foobar
  head :no_content
end

回答by Michael Kohl

Look at the headmethod:

head方法:

Return a response that has no content (merely headers). The options argument is interpreted to be a hash of header names and values.

返回一个没有内容(只有标题)的响应。options 参数被解释为标头名称和值的散列。

回答by thorsten müller

If you don't want to render anything at all you can do this:

如果你根本不想渲染任何东西,你可以这样做:

render :nothing => true, :status => 204

or like this:

或者像这样:

render :nothing => true, :status => 204 and return

Or you can use the :status => 204part with any other render command

或者您可以将该:status => 204零件与任何其他渲染命令一起使用