How can I clear out the Rails flash object after responding to an Ajax request?
Rahul's answer can be expressed more succinctly in application_controller.rb
or any other controller as:
after_action -> { flash.discard }, if: -> { request.xhr? }
This takes advantage of ActionController
's handling of lambdas and conditional filters.
For cleaning all flashes use flash.clear
instead of flash.discard
https://api.rubyonrails.org/v5.1.7/classes/ActionDispatch/Flash/FlashHash.html#method-i-clear