管理中的 AbstractController::DoubleRenderError

2024-02-19

我正在从事疯狂商务工作,我正在尝试调整 active_sale_controller 中的几个操作。我有一些条件,如果条件失败,我将重定向到(:返回),否则我将继续下一步。我现在面临的问题是我在同一个操作中使用了两次redirected_to(:back),并且在同一操作中我又使用了一个redirected_to到其他控制器,浏览器显示一个错误,内容是

"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 (:back), :notice => "problem with the start_date and 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 (:back), :notice => "more then 10 days not 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

请帮助我!


redirect_to不会停止操作方法的执行,因此如果您调用它并稍后调用render或其他redirect_to你会得到双重渲染异常。有一个相当简单的修复方法,只需调用and return. e.g.

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

请参阅《避免双重渲染异常》导轨导轨 http://guides.rubyonrails.org/layouts_and_rendering.html#avoiding-double-render-errors.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

管理中的 AbstractController::DoubleRenderError 的相关文章

随机推荐