我遇到了一个奇怪的问题,即在 Rails 4.0.2 应用程序中,Flash 消息没有在重定向之间持续存在。我怀疑这个问题与我在记录会话并获得以下输出时尝试访问闪存哈希时尚未加载的会话有关:
#<ActionDispatch::Request::Session:0x7ffda10835a8 not yet loaded>
我正在尝试更新“assets_controller”中的资产,然后如果资产已成功更新,则重定向到“dashboard_controller”的“home”操作。我的代码如下:
资产控制器:
def update
@asset = Asset.update_asset(asset_parameters, params[:id])
if @asset.errors.any?
flash.now[:error] = "There were some errors"
render 'edit'
else
flash[:success] = "Asset updated!"
logger.debug("flash in assets: #{flash.inspect}")
redirect_to root_path()
end
end
仪表板控制器:
def home
logger.debug("session #{session.inspect}")
logger.debug("flash in home #{flash.inspect}")
res = Bid.bids_query({:generation => 'current'})
@bids = []
@staging_jobs = []
res.each do |bid|
bid.staging_job_id ? @staging_jobs << bid : @bids << bid
end
end
日志输出:
Started PATCH "/assets/19" for 127.0.0.1 at 2014-03-16 21:45:53 -0700
Processing by AssetsController#update as HTML
flash in assets: #<ActionDispatch::Flash::FlashHash:0x007ff7abdcfc30 @discard=#<Set:
{}>, @flashes={:success=>"Asset updated!"}, @now=nil>
Redirected by /Users/louism2/rails_projects/rosebank-
b/app/controllers/assets_controller.rb:31:in `update'
Redirected to http://localhost:3000/
Completed 302 Found in 14ms (ActiveRecord: 2.4ms)
Started GET "/" for 127.0.0.1 at 2014-03-16 21:45:53 -0700
Processing by DashboardController#home as HTML
session #<ActionDispatch::Request::Session:0x7ffda10835a8 not yet loaded>
flash in home #<ActionDispatch::Flash::FlashHash:0x007ff7abe743e8 @discard=#<Set: {}>,
@flashes={}, @now=nil>
Completed 200 OK in 81ms (Views: 53.7ms | ActiveRecord: 2.6ms)