Sometimes all you want is to display a flash message for the current request template, i.e. it should not propagate to the next action. For example, I had an action where I wanted to warn the user that he had already processed a file:
def parse
@upload = Upload.find(params[:id])
if (@upload.processed_at)
flash[:warning] = "This file has already been processed at " + @upload.processed_at.to_s(:long)
end
end
The flash message is correctly shown at the parse.html.erb render, but it will also render on the next action processed. I do not want this to happen, so I have two options offered by the ActionController::Flash::FlashHash class.
First option is to use the now() method, which sets a flash that will not be available to the next action, only to the current.
flash.now[:warning] = "This file has already been processed at " + @upload.processed_at.to_s(:long)
The other option is the discard() method, which marks the entire flash or a single flash entry to be discarded by the end of the current action.
flash.discard # discard the entire flash at the end of the current action flash.discard(:warning) # discard only the "warning" entry at the end of the current action