Wrong color for flash messages under Rails 4.1 with Bootstrap or Foundation

僤鯓⒐⒋嵵緔 提交于 2019-11-30 00:51:30

问题


The following code displays Rails flash messages using Bootstrap 3.0:

<%# Rails flash messages styled for Twitter Bootstrap 3.0 %>
<% flash.each do |name, msg| %>
  <% if msg.is_a?(String) %>
    <div class="alert alert-<%= name == :notice ? "success" : "danger" %>">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
      <%= content_tag :div, msg, :id => "flash_#{name}" %>
    </div>
  <% end %>
<% end %>

The code is from the article Bootstrap and Rails.

Similar code from the article Foundation and Rails can be used with Foundation:

<%# Rails flash messages styled for Zurb Foundation 5.0 %>
<% flash.each do |name, msg| %>
  <% if msg.is_a?(String) %>
    <div data-alert class="alert-box round <%= name == :notice ? "success" : "alert" %>">
      <%= content_tag :div, msg %>
      <a href="#" class="close">&times;</a>
    </div>
  <% end %>
<% end %>

For either Bootstrap or Foundation, when I upgrade my application from Rails 4.0 to Rails 4.1, all flash messages appear in red, even "notice" messages which should appear in green.

What's changed in Rails 4.1 to break this code?


回答1:


Found my own answer...

I've revised my Rails and Bootstrap tutorial and updated the rails-bootstrap example app accordingly.

The Rails flash message hash contains a key (a "name") and a value (the "message").

Under Rails 4.0, the key is a Symbol.

Under Rails 4.1, the key is a String.

Styling flash messages for display with Bootstrap or Foundation requires parsing the key to determine if it is an alert or a notice.

Under Rails 4.1, names are Strings and are not matched by the code above, instead getting styled in red with the alert-danger class.

To fix it, the code for displaying flash messages with Bootstrap should change to accommodate both Rails 4.0 and Rails 4.1:

<div class="alert alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %>">

For Foundation, the code should change to this:

<div data-alert class="alert-box round <%= name.to_s == 'notice' ? 'success' : 'alert' %>">



回答2:


I tried this answer but it was marking everything as danger if it wasn't 'notice'. I ended up making a helper class to keep my other flash messages unchanged and only alter the devise ones which were returning the old boostrap 'notice' and 'alert' classes.

<div class="container">
  <% flash.each do |name, msg| %>
    <% if msg.is_a?(String) %>
      <div class="alert alert-<%= flash_class_name(name) %>" role="alert">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span>
        <span class="sr-only">Close</span>
        </button>
        <%= content_tag :div, msg, :id => "flash_#{name}" %>
      </div>
    <% end %>
  <% end %>
</div>

and a helper method

def flash_class_name(name)
    case name
    when 'notice' then 'success'
    when 'alert'  then 'danger'
    else name
    end
end


来源:https://stackoverflow.com/questions/22108286/wrong-color-for-flash-messages-under-rails-4-1-with-bootstrap-or-foundation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!