rails 4 flash not persisting across redirect "ActionDispatch::Request::Session -- not yet loaded"

StackOverflow https://stackoverflow.com/questions/22463564

  •  16-06-2023
  •  | 
  •  

Domanda

I am having a weird issue where a flash message is not persisting across redirects in a Rails 4.0.2 application. I suspect that the problem has something to do with the session not yet being loaded when I try to access the flash hash as I am logging the session and getting the following output:

#<ActionDispatch::Request::Session:0x7ffda10835a8 not yet loaded>

I am trying to update an asset in the 'assets_controller' and then redirect to the 'home' action of the 'dashboard_controller' if the asset was successfully updated. My code is as follows:

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

Log Output:

 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)
È stato utile?

Soluzione

The issue had to do with the fact that my model was named "assets" and that was somehow conflicting with the asset pipeline. The trick was to prefix the asset pipeline with an identifier other than assets which you can specify in your application.rb file with the following line:

 config.assets.prefix = '/new_name_for_asset_pipeline'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top