respond_to format.json不在rescue_from中工作

时间:2014-08-21 21:27:22

标签: ruby-on-rails ruby-on-rails-4

我有以下代码来处理ActiveRecord:我的ApplicationController中的RecordNotFound异常

  rescue_from ActiveRecord::RecordNotFound do
    respond_to do |format|
      format.json { render json: { message: 'Unable to find record' }, status: 404 }
      format.html { render 'errors/404' , status: 404 }
    end
    true
  end

但是,当收到JSON请求“Accept:application / json,text / plain, / ”时,它仍然坚持执行format.html块。如果我删除format.html块,则会引发ActionController::UnknownFormat异常。此时我被困住了。

0 个答案:

没有答案