2012-07-21 31 views
0

我正在處理spree commerce,我正在調整active_sale_controller中的一些操作。我有一些條件,如果條件失敗,我正在redirecting_to(:back),否則我正在繼續下一步。我現在面臨的問題是我使用了redirected_to(:back),兩次在同一個動作中,並且我還有一次redirected_to到同一動作中的某個其他控制器,瀏覽器顯示一個錯誤,說AdminControl中的AbstractController :: DoubleRenderError

"Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return"." 

這裏是我的代碼

when "2" 
     st_days = DateTime.strptime("#{start_date}","%d/%m/%Y %H:%M:%S") 
       ed_days = DateTime.strptime("#{end_date}","%d/%m/%Y %H:%M:%S") 
       ps = PoSale.where(:active_sale_id => @active_sale.id, :event_name => @taxon_name,:st_date => @start_date, :ed_date => @end_date).last 
      if ((st_days >= ps.st_date and st_days <= ps.ed_date) or (ed_days >= ps.st_date and ed_days <= ps.ed_date)) 

redirect_to的(:後面),:通知=> 「問題的起始日期和 END_DATE」

 else 
     PoSale.create(:active_sale_id => params[:id], :event_name => params[:active_sale]["taxon_name"], :st_date => DateTime.strptime("#{start_date}","%d/%m/%Y %H:%M:%S"), :ed_date => DateTime.strptime("#{end_date}","%d/%m/%Y %H:%M:%S")) 
      end 

     when "3" 
     puts "Inside 3" 
     puts "*"*20 
     #hidesd = DateTime.parse(params[:hide_start_date].split("+")[0]) 
     #hideed = DateTime.parse(params[:hide_end_date].split("+")[0]) 
     # hideed = DateTime.strptime("#{hide_end_date}","%d/%m/%Y %H:%M:%S") 
      puts "*"*20 
      #puts "Parameters:#{hidesd}" 
      #puts hideed 
      a_sale_id=params[:id].to_i 
      #PoSale.where("active_sale_id = 310 and st_date = '2012-07-05 03:03:00' and ed_date ='2012-07-12 08:03:00'") 
      st_days = DateTime.strptime("#{start_date}","%d/%m/%Y %H:%M:%S") 
       ed_days = DateTime.strptime("#{end_date}","%d/%m/%Y %H:%M:%S") 

       diff = (st_days.to_date - ed_days.to_date).to_i 
       if diff > 10 

redirect_to的(:後面),:通知=> 「超過10幾天沒hapenning」

    else 

      ps = PoSale.where(:active_sale_id => a_sale_id, :event_name => @taxon_name, :st_date => @start_date, :ed_date => @end_date).last #where("active_sale_id =#{a_sale_id} and st_date like ? and ed_date like ?",hidesd.strftime("%Y-%m-%d %H:%M:%S"),hideed.strftime("%Y-%m-%d %H:%M:%S")).last #use find 


      ps.update_attributes(:event_name => params[:active_sale]["taxon_name"], :st_date => DateTime.strptime("#{start_date}","%d/%m/%Y %H:%M:%S"), :ed_date => DateTime.strptime("#{end_date}","%d/%m/%Y %H:%M:%S")) 
        end 

請幫助我!

回答

6

redirect_to不會停止操作方法的執行,所以如果您調用它並稍後調用render或另一個redirect_to,您將得到雙重渲染異常。有一個相當簡單的修復,只需撥打and return即可。例如

redirect_to (:back), :notice => "problem with the start_date and end_date" and return 

參見 '避免雙重渲染例外' 的Rails guide

相關問題