Rails how to redirect if record is not found

前端 未结 5 1032
时光说笑
时光说笑 2021-02-05 05:42

I am trying to redirect if the record is not found. The page is not redirect and I get the error record not found.

My controller:

def index
@link = Link.         


        
5条回答
  •  借酒劲吻你
    2021-02-05 06:30

    You are on the right track, just capture the RecordNotFound exception:

    def index
      @link = Link.find(params[:id])
      # should render index.html.erb by default
    rescue ActiveRecord::RecordNotFound
      redirect_to(root_url, :notice => 'Record not found')
    end
    

提交回复
热议问题