处理来自 ActionController 的错误时不显示 Flash 消息

Flash message not being displayed while handling errors from ActionController

提问人:Nick Chen 提问时间:11/15/2022 最后编辑:VC.OneNick Chen 更新时间:4/28/2023 访问量:108

问:

我正在尝试使用以下代码处理来自 ActionController 的错误时显示闪存消息:

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

我还在视图/布局/应用程序.html.erb中使用以下代码定义了flash消息

    <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>

错误处理工作正常,但不显示 Flash 消息。

ruby-on-rails 错误处理 flash-message

评论


答:

0赞 Lee Drum 11/15/2022 #1

你能搬到街区外面以确保工作吗? 我刚刚尝试过,一切都很好flashbegin...rescue