我在控制器中有一行会使用自定义Flash消息重定向到主屏幕,如下所示:
redirect_to :controller => :home, :action => :index, :alert => "You are not yet assigned to an experiment." and return
然后,用户将被重定向到以下页面:http://localhost:3000/home?alert=You+are+not+yet+assigned+to+an+experiment
但是,尽管所有其他Flash消息都在我的应用程序中工作,但不显示flash消息。 views / layouts / application.html.erb 文件包含以下内容:
<div class="flash">
<%- if !notice.nil? -%>
<p class="notice"><%= notice %></p>
<%- end -%>
<%- if !alert.nil? -%>
<p class="alert"><%= alert %></p>
<%- end -%>
</div>
我在这里做错了什么?
答案 0 :(得分:5)
Flash正在使用会话来存储消息
flash[:alert] = "You are not yet assigned to an experiment."
redirect_to :controller => :home, :action => :index and return
<%= flash[:alert] %>
如果你想通过参数传递它,你应该使用params [:param_name]
redirect_to :controller => :home, :action => :index, :alert => "You are not yet assigned to an experiment." and return
<%= params[:alert] %>
更优选Flash,它在页面间隐式工作,并在显示后清除
答案 1 :(得分:2)
在您看来,您可以使用
访问通知Flash消息<%= flash[:notice] %>
不
<%= notice %>
这同样适用于警报。
此外,您的代码中存在错误。
redirect_to :controller => :home, :action => :index, :alert => "You are not yet assigned to an experiment."
必须是
redirect_to({ :controller => :home, :action => :index }, :alert => "You are not yet assigned to an experiment.")