Ruby-on-rails 强制 HTTP 响应在 Rails 中返回状态 200

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14806310/
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-02 21:24:04  来源:igfitidea点击:

Forcing HTTP Response to return Status 200 in Rails

ruby-on-railsruby-on-rails-3

提问by user1431282

How do you force your requests to return status 200 except for serious cases where I return 500? Currently, I am running into the issue where my client keeps getting a status code of 411 (length not specified) and this is causing issues with my test framework.

除了我返回 500 的严重情况外,您如何强制您的请求返回状态 200?目前,我遇到了这样一个问题,我的客户不断收到 411(未指定长度)的状态代码,这导致我的测试框架出现问题。

Is there a way to manually specify your return status in maybe a Rails controller?

有没有办法在 Rails 控制器中手动指定您的返回状态?

EDIT: More specifically I know that you can use

编辑:更具体地说,我知道你可以使用

:status

:地位

but where do I place that when using

但是在使用时我应该把它放在哪里

format.json { render :json=>final_obj}

format.json { 渲染:json=>final_obj}

to return a HTTP response after a POST?

POST 后返回 HTTP 响应?

回答by xdsemx

render status: 200, json: @controller.to_json

回答by Peter de Ridder

I suppose this should answer your question, if I understood your question correctly.

如果我正确理解了您的问题,我想这应该可以回答您的问题。

Ruby 1.9.3

红宝石 1.9.3

format.json { render json: final_obj, status: :ok }

Ruby 1.8.7

红宝石 1.8.7

format.json { render :json => final_obj, :status => :ok }