flash[:alert] 不工作,但 flash[:notice] 在重定向时显示消息

flash[:alert] not working, but flash[:notice] displays the message on redirect

我用的是rails_admingem配置如下

config.authorize_with do
  if current_user.nil? || current_user.role != 'admin'
    redirect_to main_app.root_path
    flash[:alert] = "Sorry you are not authorized!"
  end
end

当我使用 flash[:notice] 时,我可以在 root_path 上看到消息,但是如果我将其更改为 flash[:alert],它不会显示,任何想法为什么以及会是什么解决方案?

我只想使用 :alert,因为更改通知颜色会导致所有其他通知显示红色字体。

您可能没有将 flash[:alert] 散列输出到您的页面。

在您的视图中查看您输出 flash[:notice] 的位置(可能在 app/views/layouts/application.html.erb(或类似位置)并复制您要提醒的通知行。它应该看起来像像这样:

<% if flash[:alert] %>
      <div id="alert">
         <%= flash[:alert] %> 
      </div>
<% end %>

如果不存在类似的东西(或者您找不到它),请将其添加到您的视图(或布局)