Display better errors on rails apps with bootstrap.
Add to your Gemfile
gem 'user_error'
The engine will catch every exception defined in the ActionDispatch::ExceptionWrapper middleware
and set up an exceptions_app to handle them.
To make it catch your custom exceptions, create a new initializer in config/initializers with the following contents:
UserError.configure do
rescue_from 'My::Exception', :with => :forbidden
end
An initializer will help you on that, if you want. Just execute
railg g user_error:install
e.g. for CanCan:
UserError.configure do
rescue_from 'CanCan::AccessDenied', :with => :forbidden
end
The :with value will define the HTTP status code of the response, as defined in Rack::Utils,
and it will also be an I18n namespace to customize the messages.
If you want to customize the UserError controller, do it in a controller
block. E.g. if you have an :authenticate callback in your ApplicationController
but you do not want the exception display to require :authenticate, you could do:
UserError.configure do
controller do
skip_before_filter :authenticate
end
end
Of course, make sure that your application layout can deal with a current_user being nil.
- Fork the repository on Github
- Create a named feature branch (like
add_component_x) - Write you change
- Write tests for your change (if applicable)
- Run the tests, ensuring they all pass
- Submit a Pull Request using Github
Lleir Borras http://github.com/lleirborras
Marcello Barnaba http://github.com/vjt
This software is Made in Italy 🇮🇹 😄.