Inside the controller, you can use rescue_from only for errors that occur inside your controller (in actions, views or filters).
It seems that ActionController::BadRequest rises before the routing passes the request to the controller (somewhere on the middleware stack).
You can handle such errors if you write your own middleware as follows:
class HandleErrorsMiddleware def initialize(app) @app = app end def call(env) @app.call(env) rescue ActionController::BadRequest ApplicationController.action(:raise_bad_request).call(env) end end
raise_bad_request must be a public method in ApplicationController
You must add this middleware to config/application.rb
config.middleware.insert_before 'ActionDispatch::ParamsParser', 'HandleErrorsMiddleware'
source share