Rails has this functionality built in already. If you want to show a 404 page, create a
render_404 method (or
not_found ) in
ApplicationController like this:
def not_found raise ActionController::RoutingError.new('Not Found') end
Rails also handles
ActiveRecord::RecordNotFound the same way.
This does two things better:
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:
user = User.find_by_email(params[:email]) or not_found user.do_something!
without having to write ugly conditional statements.
As a bonus, it's also super easy to handle in tests.