<% flash.each do |key, msg| %>
<% if key == 'notice' %>
<%= content_tag :div, "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button> #{key}".html_safe, class: 'alert alert-success alert-dismissable' %>
<% elsif key == 'alert' %>
<%= content_tag :div, "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button> #{msg}".html_safe, class: 'alert alert-danger alert-dismissable' %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
不适合我.
<% if true %>时候.key,它说notice.为什么不工作?
Kir*_*rat 11
对于Rails 4和以下,闪存key是一个Symbol和Rails 4.1闪存key是一个String.我想你正在使用Rails 4 or under和比较他们的String等价物,这是你问题的根源.
您可以通过两种方式解决它:
将键从Symbol转换为String,然后将其与String notice或alert.这种方式更好,因为稍后当您将Rails版本升级到4.1时,您将不会再遇到当前问题.
<% if key.to_s == 'notice' %>
<%# .. %>
<% elsif key.to_s == 'alert' %>
<%# ..%>
<% end %>
Run Code Online (Sandbox Code Playgroud)直接与符号:notice和比较:alert.这将暂时起作用,但在升级Rails版本时不起作用.
<% if key == :notice %>
<%# .. %>
<% elsif key == :alert %>
<%# ..%>
<% end %>
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
166 次 |
| 最近记录: |