ruby on rails - rescue_from not working with ActionController::BadRequest -
using Rail 4, I'll work with application_controller.rb inside the controller You have to It looks like If you write your middle class this way, then you can take action on such errors: You must add this middleware to rescue_from with
ActionController :: BadRequest can not be found for :
rescue_from ActionController :: BadRequest, with: raise_bad_request def raise_bad_request (Nothing present: true, position: 404) end
rescue_from to collect errors only (in action, views or filters) inside your controller.
Passes the ActionController :: BadRequest route (middleware stack before requesting to do the controller somewhere).
class HandleErrorsMiddleware def initialize (app) @app = app End DIF Call (env) @ app call (env) Rescue control: BadRequest ApplicationController.action (raise_bad_request) .call (env) End End
raise_bad_request
ApplicationController
config / application.rb
config.middleware .insert_before 'ActionDispatch :: ParamsParser', 'HandleErrorsMiddleware'
Comments
Post a Comment