2012-02-03 44 views
4

是否有方法将类或标识添加到特定的Flash消息?我需要让一些消息消失,一些消息继续存在。我想基于一个html类来做到这一点。添加标识或类到flash消息

感谢所有帮助

回答

3

没有什么神奇的东西可以把闪光灯。这只是一个散列在会议中并为你清除。

你可以(例如)做

flash[:notice] = {:class => :urgent, :body => 'hello'} 

,然后在布局

- if flash[:notice] 
    %div{:class => flash[:notice][:class]} 
    = flash[:notice][:body] 

但绝对要做到这一点不止一种方法 - 你可以只使用一个类时,它的flash[:notice] ,另一个用于flash[:error]

0

添加类:

<%= content_tag(:p, notice, class: 'notice') if notice %> 
<%= content_tag(:p, alert, class: 'alert') if alert %> 

添加ID:

<%= content_tag(:p, notice, id: 'notice') if notice %> 
<%= content_tag(:p, alert, id: 'alert') if alert %> 

它使得(例如):

<p class="alert">Invalid email or password.</p> 
<p id="alert">Invalid email or password.</p> 
3

的Flash消息简单地存储在一个散列,flash。在你看来,你可能会遍历所有Flash的消息是这样的:

<% flash.each do |key, msg| %> 
    <%= content_tag :div, msg, :id => key %> 
<% end %> 

你总是可以检查特定的消息,并添加特定的类。可能是这样的:

<% flash.each do |key, msg| %> 
    <% if msg.include? 'fatal' %> 
    <%= content_tag :div, msg, :id => key, :class => 'fatal' %> 
    <% end %> 
<% end %> 

这是一个railscast,提供更多关于flash消息的信息。

http://railscasts.com/episodes/18-looping-through-flash