繁体   English   中英

使用respond_with来服务404?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM