Reputation: 110950
I'm building a Rails 3 app on Heroku. Right now my error pages and 404 page are all standard rails/heroku pages.
I'd like to customize these two. Have a page for an error, and have a page for a 404.
All the tutorials found on google are years old, and don't work on Rails 3.
Can you please suggest a good modern tutorial or tips on how to do the above?
Thanks
Upvotes: 27
Views: 15000
Reputation: 41874
2013 update for Rails 3.2 from Jose Valim
When Rails 3.0 came out, one of the features that people suddenly missed was the ability to better handle exceptions. The issue was: since Rails 3 became a lot more Rack “fluent”, we had to move some features to the middleware stack and this forced us to move the whole exceptions handling as well. Rails 3.2 attempts to bring some customization back to the game by allowing you to set your own exceptions rack application that is invoked when a failure happens. For instance, you could set the exceptions application to your own router in your config/application.rb:
config.exceptions_app = self.routes
Now, every time there is an exception, your router is going to be invoked. Therefore, to render custom 404 pages, you could simply add to your router:
match "/404", :to => "errors#not_found"
And implement the logic in the controller as you wish! However, there are a few things to keep in mind if you go down this road:
rails server -e production
]Remember that whatever you do in the errors controller, it should not be anything “fancy”. Keep it simple because something already went wrong with your application!
Upvotes: 11
Reputation: 697
I found this tutorial quite useful:
http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages
Upvotes: 11
Reputation: 107718
If you want to capture specific errors, use rescue_from
in ApplicationController
.
Otherwise if you just want to edit the default error pages, edit the 500.html and 400.html files in {Rails.root}/public
Upvotes: 36
Reputation: 32335
Well rails3 still uses the same 404.html, 422.html and 500.html in the public folder. You can customize those.
If you're talking about actually catching these exceptions, and doing some dynamic stuff, I think the basic functionality is the same, have some around_filter
that catches your particular exception in application_controller
ie ActiveRecord::RecordNotFound
and do something with that.
Upvotes: 3