Flash message not being displayed while handling errors from ActionController

131 Views Asked by At

I am trying to display flash message while handling errors from ActionController with the following code:

class ActionController < ApplicationController
    def action
      begin
        @results = ApiHelper.helper(paremeter)
      rescue StandardError => e
        flash[:danger] = "show some error messages"
      end
      render "something"
    end
end

I also define the flash message with the following code in views/layouts/applications.html.erb

    <body>
    <%= render 'layouts/header' %>
    <div class="container">
    <% flash.each do |message_type, message| %>
    <div class="alert alert-<%= message_type %>"><%= message %></div>
    <% end %>
    <%= yield %>
    <%= render 'layouts/footer' %>
    <%= debug(params) if Rails.env.development? %>
    </div>
    </body>

Error handling works fine, but flash messages not being displayed.

1

There are 1 best solutions below

0
Lee Drum On

Could you move flash outside begin...rescue block to make sure working? I have just tried and everything is fine