【问题标题】:rails 4 flash not persisting across redirect "ActionDispatch::Request::Session -- not yet loaded"rails 4 flash 不会在重定向“ActionDispatch::Request::Session -- 尚未加载”中持续存在
【发布时间】:2014-03-17 19:27:02
【问题描述】:

我遇到了一个奇怪的问题,即 Flash 消息在 Rails 4.0.2 应用程序中的重定向中没有持续存在。我怀疑这个问题与我在记录会话并获得以下输出时尝试访问闪存哈希时尚未加载的会话有关:

#<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)

【问题讨论】:

    标签: ruby-on-rails-4 actiondispatch


    【解决方案1】:

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

     config.assets.prefix = '/new_name_for_asset_pipeline'
    

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 2015-02-15
      • 2015-11-15
      • 2014-01-28
      • 1970-01-01
      • 2014-08-26
      相关资源
      最近更新 更多