gpt4 book ai didi

ruby-on-rails - 在 Rails 中发送 flash 通知不会在重定向时弹出

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

我在 ApplicationController 方法中有一个重定向,并希望通过以下方式发送通知:

class ApplicationController < ActionController::Base

def redirect_if_no_user
if current_user.nil?
redirect_to root_path, notice: t('errors.session_expired')
end
end

end

我在其他一些 Controller 操作中调用 redirect_if_no_user

不幸的是,在我手动在主页上重新加载之前我看不到通知(在我已经通过该方法重定向到它之后)。这种行为是故意的吗?有人有想法吗?

最佳答案

来自 rails 文档,( http://guides.rubyonrails.org/action_controller_overview.html#the-flash )

默认情况下,将值添加到闪存将使它们可用于下一个请求,但有时您可能希望在同一请求中访问这些值。例如,如果创建操作未能保存资源,而您直接呈现新模板,则不会产生新请求,但您可能仍想使用 Flash 显示消息。为此,您可以像使用普通 Flash 一样使用 flash.now:

flash.now[:notice] = t('errors.session_expired')
redirect_to root_path

关于ruby-on-rails - 在 Rails 中发送 flash 通知不会在重定向时弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35206187/

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