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.
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:
raise ActionController::RoutingError.new('Not Found')
Rails also handles
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:
user = User.find_by_email(params[:email]) or not_found
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.