Here's the code for sending a json response associated with a working and non working status. Does anyone have recommendations for other resources? This isn't working. Thank you.
这是发送与工作和非工作状态相关的json响应的代码。有没有人有其他资源的建议?这不起作用。谢谢。
if @content.destroy
format.json { redirect_to @collection, status: :destroyed, notice 'Content was removed from collection.' }, :status => 200
else
format.json { render json @content.errors, status: :unprocessable_entity }, :status => 400
end
1 个解决方案
#1
-1
TL;DR: if you are using some JS framework, then look up how that framework wants success and failure response to look like, if you are rolling your own - then do whatever makes more sense for you.
TL; DR:如果您正在使用一些JS框架,那么请查看该框架如何想要成功和失败响应,如果您正在自己滚动 - 那么做一些对您更有意义的事情。
Long version:
There are couple ways you can return response from rails to your web app. Rails by default returns status code 2xx for success and 4xx for failed requests. Some web frameworks such as Extjs, like to receive response with 200 code always and look at success key in the response to see if the request was successfult. If you are writing your own JS that utilizes XHR, then you can do whatever you want: its up to you how you architect your API. I find it useful to return json response from server with 200 code and set success key to true or false so in my callback JS i can write something like:
有几种方法可以将响应从rails返回到您的Web应用程序。默认情况下,Rails返回状态代码2xx表示成功,4xx表示失败请求。一些Web框架(如Extjs)总是喜欢接收带有200个代码的响应,并在响应中查看成功键以查看请求是否成功。如果您正在编写自己的使用XHR的JS,那么您可以随心所欲:由您自己构建API。我发现从服务器返回带有200个代码的json响应并将成功键设置为true或false很有用所以在我的回调JS中我可以编写如下内容:
if(request.data.success){
successfulPost(response);
} else {
failed(response);
}
one of the reasons I prefer that approach is that, if there is an exception, then I can actually distinguish from record not being saved due to validation errors as opposed to bug in code that caused exception. But that is a personal preference and some may argue that error is error and it does not matter what caused it.
我更喜欢这种方法的原因之一是,如果存在异常,那么我实际上可以区分由于验证错误而未保存的记录而不是导致异常的代码中的错误。但这是个人偏好,有些人可能认为错误是错误的,导致它的原因并不重要。
#1
-1
TL;DR: if you are using some JS framework, then look up how that framework wants success and failure response to look like, if you are rolling your own - then do whatever makes more sense for you.
TL; DR:如果您正在使用一些JS框架,那么请查看该框架如何想要成功和失败响应,如果您正在自己滚动 - 那么做一些对您更有意义的事情。
Long version:
There are couple ways you can return response from rails to your web app. Rails by default returns status code 2xx for success and 4xx for failed requests. Some web frameworks such as Extjs, like to receive response with 200 code always and look at success key in the response to see if the request was successfult. If you are writing your own JS that utilizes XHR, then you can do whatever you want: its up to you how you architect your API. I find it useful to return json response from server with 200 code and set success key to true or false so in my callback JS i can write something like:
有几种方法可以将响应从rails返回到您的Web应用程序。默认情况下,Rails返回状态代码2xx表示成功,4xx表示失败请求。一些Web框架(如Extjs)总是喜欢接收带有200个代码的响应,并在响应中查看成功键以查看请求是否成功。如果您正在编写自己的使用XHR的JS,那么您可以随心所欲:由您自己构建API。我发现从服务器返回带有200个代码的json响应并将成功键设置为true或false很有用所以在我的回调JS中我可以编写如下内容:
if(request.data.success){
successfulPost(response);
} else {
failed(response);
}
one of the reasons I prefer that approach is that, if there is an exception, then I can actually distinguish from record not being saved due to validation errors as opposed to bug in code that caused exception. But that is a personal preference and some may argue that error is error and it does not matter what caused it.
我更喜欢这种方法的原因之一是,如果存在异常,那么我实际上可以区分由于验证错误而未保存的记录而不是导致异常的代码中的错误。但这是个人偏好,有些人可能认为错误是错误的,导致它的原因并不重要。