我遇到了一个奇怪的问题,在Rails 4.0.2应用程序中,flash消息在重定向中不持久。我怀疑这个问题与会话尚未加载有关,当我尝试访问flash哈希时,我正在记录会话并获得以下输出:
#<ActionDispatch::Request::Session:0x7ffda10835a8 not yet loaded>
我试图更新'assets_controller'中的资产,然后重定向到'dashboard_controller'的'home'动作,如果资产已成功更新。我的代码如下:
AssetsController:
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
DashboardController:
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)
这个问题与我的模型被命名为"assets"这一事实有关,这在某种程度上与资产管道相冲突。诀窍是给资产管道加上一个标识符,而不是assets,您可以在应用程序中指定它。
config.assets.prefix = '/new_name_for_asset_pipeline'