开发者

AbstractController::DoubleRenderError in rails 3

开发者 https://www.devze.com 2023-03-25 01:23 出处:网络
I always get the following error: AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that 开发者_如何学Pythonyou may only

I always get the following error:

AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that 开发者_如何学Pythonyou 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".):

The error happens, when it id is nil the first, but not the second time...

def calc_next
  id = next()
  if id.nil?
    id = next_next()
    if id.nil?
      render :layout => false, :format => :js
    else
      redirect_to :action => "view", :id => id, :format => :js
    end
  else
    redirect_to :action => "view", :id => id, :format => :js
  end
end

I don't see the problem in this redirection, as the outer one is fine. Even with the debugger there are not two redirections at the same time...

Any help is appreciated... Markus


This looks like some kind of helper function rather than a Controller action. In which case you're probably calling calc_next twice in one action, or render / redirect_to from somewhere else in the same action. Remember that render and redirect_to don't immediately cause the Controller to return.

Check whether your control path can both call calc_next and call render or redirect_to from somewhere else (or from a second call into calc_next).

If you post the controller action you're going through, we may be able to help better.


Are there any before_filters that might be rendering or redirecting?


You have two options also:

redirect ... and return render ... and return

0

精彩评论

暂无评论...
验证码 换一张
取 消