Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redirect to 404 page when any records do not exist?

eg. I have student and teacher model. if i visited the student or teacher profile that not exists in db it will raise as ActiveRecord::RecordNotFound.

I have tried this in the application controller but still got an error message ActiveRecord::RecordNotFound instead of 404 page.

class ApplicationController < ActionController::Base
  rescue_from ActiveRecord::RecordNotFound, with: :render_404
end

So, is there a solution for this?

Thanks.

like image 630
Parinha Avatar asked Nov 24 '25 20:11

Parinha


1 Answers

try this :)

    class ApplicationController < ActionController::Base
      rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

      private

      def record_not_found
        render file: "#{Rails.root}/public/404", layout: true, status: :not_found
      end
    end
like image 120
Osp Avatar answered Nov 27 '25 10:11

Osp