Search code examples
ruby-on-railsrubytwitter-bootstrapflash-message

Flash Message Customization


In my application.rb:

<% flash.each do |name, msg| %>
  <%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>

which works for me because I have about 10 different flash lines of code, but on only one of them I want the flash to display as red

flash[:danger] = 'Striked added' # MAKE RED

Do you know a quick fix?


Solution

  • You can use string interpolation in the :class option with a conditional statement to add another class for only a certain kind of flash:

    <% flash.each do |name, msg| %>
      <%= content_tag(:div, msg, class: "alert alert-info #{'red' if name == :danger}") %>
    <% end %>