如何在Rails控制器中返回HTTP 204

Non*_*nos 56 ruby ruby-on-rails

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

respond_to do |format|
  format.html  
end
Run Code Online (Sandbox Code Playgroud)

返回204?

Eli*_*kes 97

head :no_content
Run Code Online (Sandbox Code Playgroud)

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

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

def foobar
  head :no_content
end
Run Code Online (Sandbox Code Playgroud)


Mic*_*ohl 11

看看head方法:

返回没有内容的响应(仅限标题).options参数被解释为标题名称和值的哈希值.

  • 我想我找到了:head :no_content 或 head 204。谢谢! (2认同)

tho*_*ler 7

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

render :nothing => true, :status => 204
Run Code Online (Sandbox Code Playgroud)

或者像这样:

render :nothing => true, :status => 204 and return
Run Code Online (Sandbox Code Playgroud)

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