How to use flash with error handler in Rails 6?

Viewed 92

For some reason, the flash message does not work when rescuing from an error.

routes.rb:

  get '/test', :to => 'pages#test', :via => :all
  match '/500', :to => 'errors#internal_server_error', :via => :all

application.rb:

module RailsApp
  class Application < Rails::Application
    config.exceptions_app = self.routes

pages_controller.rb

class PagesController < ApplicationController
  def test
    raise 'Test error'
  end

errors_controller.rb:

class ErrorsController < ApplicationController
  def internal_server_error
    flash[:notice] = 'Internal server error. Please try again later.'
    redirect_to root_url
  end

Visiting /500 redirects and renders the expected flash, but visiting /test does not.

Visiting /test redirects to root but does not render the flash message.

How to render the flash message when catching an exception?

0 Answers
Related