gpt4 book ai didi

ruby-on-rails - 如何识别 Rails 中 flash[] 消息的类型

转载 作者:行者123 更新时间:2023-12-04 06:05:15 24 4
gpt4 key购买 nike

我想对不同类型的 Flash 消息有不同的风格:错误、通知、成功。
我如何识别传递给我的 View 的 Flash 消息的类型?

在我的 Controller 中,我有:

flash[:error] = "Access denied."

在我的 application.html.haml 中,我有:
  - if not flash.empty?
- flash.each do |key, value|
%div{:class => "alert-message #{key}"}= value

谢谢你。

最佳答案

flash本质上是基于散列。每种 flash 消息只是与该类型的符号键关联的值。所以flash[:error]用于错误消息,flash[:notice]flash[:success]对于他们的关键类型。您甚至可以定义自己的类型(只需使用任何符号)。并且您可以一次设置任意数量的键(例如,您可以在同一请求中设置 flash[:notice]flash[:error])。

在您的示例中,这是创建一个 div与类(class) alert-message #{key} .因此,例如,如果您设置了 flash[:notice]输出将是:

<div class="alert-message notice">Notice message</div>

它会为您设置的每种类型打印一个。所以如果请求设置 flash[:error] = 'something went terribly wrong'flash[:notice] = 'take a look around' ,你会从那个模板中得到这个 html:
<div class="alert-message error">something went terribly wrong</div>
<div class="alert-message notice">take a look around</div>

关于ruby-on-rails - 如何识别 Rails 中 flash[] 消息的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8441201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com