I’d like to ‘fake’ a 404 page in Rails. In PHP, I would just send a header with the error code as such:
header("HTTP/1.0 404 Not Found");
How is that done with Rails?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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_404method (ornot_foundas I called it) inApplicationControllerlike this:Rails also handles
AbstractController::ActionNotFound, andActiveRecord::RecordNotFoundthe same way.This does two things better:
1) It uses Rails’ built in
rescue_fromhandler to render the 404 page, and2) it interrupts the execution of your code, letting you do nice things like:
without having to write ugly conditional statements.
As a bonus, it’s also super easy to handle in tests. For example, in an rspec integration test:
And minitest:
OR refer more info from Rails render 404 not found from a controller action