How to redirect to a 404 in Rails?

Posted by Rohit Kumar | Updated on

To return a 404 header, just use the :status option for the render method. If you want to render the standard 404 page you can extract the feature in a method.

How to redirect to 404 in Rails?

Don’t render 404 yourself, there’s no reason to; Rails has this functionality built in already. If you want to show a 404 page, create a render_404 method (or not_found as I called it) in ApplicationController like this:

Rails also handles AbstractController::ActionNotFound, and ActiveRecord::RecordNotFound the same way. This does two things better:

1) It uses Rails’ built in rescue_from handler to render the 404 page, and 2) it interrupts the execution of your code, letting you do nice things like:

If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@] Your question and answer will appear on and help other developers.

Related Questions & Answers