When going to on object's show page with an id that doesn't exist, the RecordNotFonud
exception is thown. Is there a way I can redirect to some error page, or maybe a different action when this error is thrown?
You may use rescue_from if you are using Rails 3:
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
def render_404
respond_to do |format|
format.html { render :action => "errors/404.html.erb", :status => 404 }
# and so on..
end
end
end
Yes, you can also do a redirect instead of render, but this is not a good idea. Any semi-automatic interaction with your site will think that the transfer was successfull (because the returned code was not 404), but the received resource was not the one your client wanted.
In development mode you'll see the exception details but it should automatically render the 404.html file from your public directory when your app is running in production mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With