[英]Using respond_with to serve 404?
我正在尝试在rails中提供自定义404响应,但我不确定如何将:status赋予respond_with
函数。
我知道render
,我可以这样做:
render :status => :not_found, :json => {:response =>"There weren't any results"}
如何使用respond_with
做类似的事情? 有没有办法用respond_with
设置状态代码?
我使用的唯一原因respond_with
是因为,我的理解,它是使用适当的协议respond_with
当你开始使用respond_to
。 如果这不正确,我应该使用render
,请告诉我!
查看文档 ; respond_with
接受:status
作为选项的:status
。
respond_with(@user, status: :not_found) do |format|
format.json { response: "There weren't any results" }
end
您可以只提出一个ActiveRecord::RecordNotFound
异常,而不是在您的响应中设置特定的404状态,它具有相同的效果。 在这里查看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.