2017-06-21 45 views
2

在我的应用程序中,我以这种方式向用户显示消息。Ruby on Rails自举警报框

<% flash.each do |key, value| %> 
    <div class="alert alert-<%= key %> alert-dismissible"> 
     <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
     <%= value %> 
    </div> 
<% end %> 

我有以下线在我的应用程序

.alert-notice { 
    @extend .alert-warning; 
} 

当我重定向用户使用redirect_to root_url,:通知=> '邮件' 一切都完美。可以看到通知消息。但是,当我指导用户使用redirect_to root_url,:info =>'messages'时,无法看到任何消息。我调试了代码,并意识到闪存是空的那种情况。

这是确定:

redirect_to root_url, :notice => 'messages' 

这里是问题:

redirect_to root_url, :info => 'messages' 

有什么建议?

谢谢。

回答

0

:info不是redirect的vaild选项(只有:alert:notice可以这样使用);因此,要使用info,您必须将其直接分配给flash。如果你想使用info

redirect_to root_url, flash: { info: 'messages' } 
+0

亲爱downvoter,我会感谢我的回答的缺点,所以任何见解我有机会变得更好。 – Gerry

2

你必须把它添加到application_helper.rb

add_flash_types :info 

您可以添加尽可能多的:

试试这个:

flash[:info] = 'messages' 
redirect_to root_url 

或者这你想要的额外类型,例如

add_flash_types :info, :success, :warning, :danger 

附加:

我也建议你使用新的HAST符号在Ruby中

redirect_to root_url, info: 'messages' # instead of :info => ... 
+1

我不知道'add_flash_types',很好。 – Gerry