gpt4 book ai didi

ruby-on-rails - session 中缺少 Rails 闪存

转载 作者:行者123 更新时间:2023-12-03 12:01:41 29 4
gpt4 key购买 nike

我从 Rails 3.2 升级到 4.0,现在重定向后我的 flash 消息都不起作用。
我检查过以确保我显示 Flash 消息的方式没有问题。不涉及重定向的 Flash 消息工作正常。
session 中缺少 Flash
我仔细研究了一下,发现了一些有趣的事情。如果我放一个 Pry在我重定向到的 Controller 操作上,我可以看到 session["flash"]nil .我不相信session["flash"]应该是 nil在那时候。
如果我输入 session.keys进入控制台,它向我展示了这个:

["session_id", "kmq", "warden.user.user.key", "_csrf_token", "foo"]
# "foo" is something I added myself as a test
不应该有一个名为 "flash" 的 session key 吗? ?
环境中缺少 Flash
我看 FlashHash source code我看到 Flash 消息显然不仅保存到 session 中,而且还保存到 @env .于是我进行了下面的实验。
# I'm manually setting a flash message
> flash[:notice] = "hello"

# Now I'm checking for the flash message in env
> env["action_dispatch.request.flash_hash"]

=> #<ActionDispatch::Flash::FlashHash:0x007fda544718b0
@discard=#<Set: {}>,
@flashes={:notice=>"hello"},
@now=nil>

# Now I'm checking for it in the session
> session["flash"]
=> nil
然后,如果我对重定向到的 Controller 操作执行 Pry 操作,则可以执行以下操作:
> env["action_dispatch.request.flash_hash"]
它返回 nil .
环境有时, session 从不
我的 HashFlash显然使它成为 env有时, session绝不。它永远不会进入的事实 session我觉得可能是不正确的行为。
我的重定向代码
def update
user_params.delete(:password) if user_params && user_params[:password].blank?
if user.update_attributes(user_params)
respond_to do |format|
format.html do
redirect_to(user_account_path(user, anchor: params[:tab]), notice: "Your account has been updated")
end
format.js
end
else
render :show
end
end
我知道控制正在重定向的事实。我也试过将代码更改为这个,但没有运气。
def update
# Redirect right away to kill the extra moving parts
redirect_to(user_account_path(user, anchor: params[:tab]), notice: "Your account has been updated")
return

user_params.delete(:password) if user_params && user_params[:password].blank?
if user.update_attributes(user_params)
respond_to do |format|
format.html do
redirect_to(user_account_path(user, anchor: params[:tab]), notice: "Your account has been updated")
end
format.js
end
else
render :show
end
end
所以我的问题是: 我怎样才能让我的 Flash 消息再次工作?
编辑 : 当我手动设置 session["flash"]flash.to_session_value ,闪现信息出现了,尽管它永久“卡住”了。不过,这似乎是一个线索。
编辑 2 :通过对一个已知的工作项目进行一些检查,我了解到 session["flash"]永远不可能在为 flash[:notice] 赋值后立即出现(或任何其他键)。显然,Flash 消息仅插入 session Controller Action 返回后。这样就解开了那部分谜团。
编辑 3 :FWIW,这是 rake middleware 的输出:
use Airbrake::UserInformer
use Rack::Sendfile
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007fcc23268eb8>
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use BetterErrors::Middleware
use Airbrake::Rails::Middleware
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActiveRecord::Migration::CheckPending
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::ActiveRecordStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use Warden::Manager
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use OmniAuth::Builder
run DueProps::Application.routes
我不知道这是否提供了任何线索。

最佳答案

我找到了罪魁祸首。有人在 config/application.rb 中添加了这个:

use ActionDispatch::Cookies
use ActionDispatch::Session::ActiveRecordStore

因此, rake middleware正在展示两个 ActionDispatch::CookiesActionDispatch::Session :
use ActionDispatch::Cookies
use ActionDispatch::Session::ActiveRecordStore

use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore

一旦我们删除了这两行,Flash 消息就会再次起作用。

我必须感谢@jbgo,因为他的评论使我找到了解决方案。

关于ruby-on-rails - session 中缺少 Rails 闪存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29122340/

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