使用respond_with来服务404?

Ril*_*eyE 3 ruby-on-rails http-status-codes respond-with

我正在尝试在rails中提供自定义404响应,但我不确定如何将:status赋予respond_with函数.

我知道render,我可以这样做:

render :status => :not_found, :json => {:response =>"There weren't any results"}
Run Code Online (Sandbox Code Playgroud)

我怎么能做那样的事情respond_with?有没有办法设置状态代码respond_with

我使用的唯一原因respond_with是因为,根据我的理解,respond_with当你开始使用时,它是适当的协议respond_to.如果这不正确我应该使用render,请告诉我!

dee*_*our 7

查看文档 ; respond_with需要:status作为一个选项.

respond_with(@user, status: :not_found) do |format|
  format.json { response: "There weren't any results" }
end
Run Code Online (Sandbox Code Playgroud)