rails 4 flash不会持久存在重定向“ActionDispatch :: Request :: Session - 尚未加载”

时间:2014-03-17 19:27:02

标签: ruby-on-rails-4 actiondispatch

我遇到一个奇怪的问题,即FLails 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)

1 个答案:

答案 0 :(得分:0)

这个问题与我的模型被命名为“资产”并且与资产管道产生某种冲突的事实有关。诀窍是在资产管道前添加 assets 以外的标识符,您可以在 application.rb 文件中指定该标识符,其中包含以下行:

 config.assets.prefix = '/new_name_for_asset_pipeline'