gpt4 book ai didi

ruby-on-rails - Rails Flash 消息未显示

转载 作者:数据小太阳 更新时间:2023-10-29 08:25:12 36 4
gpt4 key购买 nike

我在 Controller 中有一行将重定向到带有自定义 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

但是,虽然所有其他的闪现消息在我的应用程序中都有效,但闪现消息并没有显示出来。 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>

我在这里做错了什么?

最佳答案

Flash 正在使用 session 来存储消息

flash[:alert] = "You are not yet assigned to an experiment."
redirect_to :controller => :home, :action => :index and return
<%= flash[:alert] %>

如果你想通过 params 传递它,你应该使用 params[:param_name]

redirect_to :controller => :home, :action => :index, :alert => "You are not yet assigned to an experiment." and return
<%= params[:alert] %>

Flash更可取,它隐含地跨页面工作,显示后被清除

关于ruby-on-rails - Rails Flash 消息未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4407719/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com