General rescue throughout controller when id not found - RoR

后端 未结 4 1909
野的像风
野的像风 2021-02-07 11:21

I have stumbled upon a situation where my application looks for an id that does not exist in the database. An exception is thrown. Of course, this is a pretty standard situation

4条回答
  •  攒了一身酷
    2021-02-07 11:51

    You must use rescue_from for this task. See example in the Action Controller Overview Guide

    class ApplicationController < ActionController::Base
      rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
    
      private
    
      def record_not_found
        redirect_to action: :index
      end
    end
    

提交回复
热议问题