Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails中识别flash[]消息的类型_Ruby On Rails_Json - Fatal编程技术网

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

Ruby on rails 如何在Rails中识别flash[]消息的类型,ruby-on-rails,json,Ruby On Rails,Json,我想有不同的风格为不同类型的闪光消息:错误,通知,成功。 如何识别传递给视图的flash消息的类型 在我的控制器中,我有: 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] = "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]

在您的示例中,这是使用类
警报消息#{key}
创建一个
div
。因此,例如,如果设置一个
flash[:notice]
,输出将是:

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

看起来你已经在学习CSS类了?谢谢Ben。这正是我想要的答案。
<div class="alert-message error">something went terribly wrong</div>
<div class="alert-message notice">take a look around</div>