I would like to have different style for different type of flash messages: error, notice, success.
How would I identify the type of flash messages passed to my view?
In my controller, I have:
flash[:error] = "Access denied."
In my application.html.haml, I have:
- if not flash.empty?
- flash.each do |key, value|
%div{:class => "alert-message #{key}"}= value
Thank you.
flashis essentially based on a hash. Each kind of flash message is just the value associated with a symbol key for that type. Soflash[:error]is for error messages,flash[:notice]andflash[:success]for their key types. You can even define your own types (just using any symbol). And you can have any number of keys set at once (so you can for example set aflash[:notice]andflash[:error]in the same request).In your example, this is creating a
divwith the classalert-message #{key}. So, for example, if you set aflash[:notice]the output would be:And it will print one for each type you set. So if the request set
flash[:error] = 'something went terribly wrong'andflash[:notice] = 'take a look around', you’d get this html from that template: