双重渲染错误轨道

不确定如何获得此错误:

AbstractController::DoubleRenderError users#create 

在我的控制器中,我得到了这个代码:

 render 'new' and return 

我从bugsnag得到了日志,说我在这一行得到了错误。

这是创建方法代码:

 def create back_button and return if params[:back_button] @profile = current_user.build_profile(params[:user]) if @profile.nil? || current_user.nil? || @profile.user.nil? sign_out redirect_to signup_path and return end if @profile.new_record? render 'new' and return else redirect_to more_questions_path and return end end 

我在此控制器中进行过滤之前:

 before_filter :signed_in_user def signed_in_user unless signed_in? store_location redirect_to signin_url, notice: "Please sign in." end end 

尝试一下:

 class UsersController < ApplicationController before_filter :signed_in_user def create return back_button if params[:back_button] @profile = current_user.build_profile(params[:user]) if @profile.nil? || current_user.nil? || @profile.user.nil? sign_out return redirect_to signup_path end if @profile.new_record? render 'new' else redirect_to more_questions_path end end private def signed_in_user unless signed_in? store_location return redirect_to signin_url, notice: "Please sign in." end end end 

背后的原因: x and return表示x and return nil ,因此返回nil 。 实际上,您尝试将控制器操作短路,并return redirect_to ...

并没有为你做任何事情。

在你拥有的每个地方

 xxx and return 

用它替换它

 xxx return 

对于exameple:

 redirect_to signup_path return 

这应该更像你期望的那样

你有一个渲染和重定向。 你必须选一个。

我想redirect_to signup_path返回nilfalse ,因此你的and return没有被执行。

您可以通过多种方式解决,最简单的方法是替换

 redirect_to signup_path and return 

通过

 redirect_to signup_path return 

但是,我建议你做一个更大的改变。 尝试改变这个

 if @profile.nil? || current_user.nil? || @profile.user.nil? sign_out redirect_to signup_path and return end if @profile.new_record? render 'new' and return else redirect_to more_questions_path and return end 

通过

 if @profile.nil? || current_user.nil? || @profile.user.nil? sign_out redirect_to signup_path elsif @profile.new_record? render 'new' else redirect_to more_questions_path end 

这样很明显,只需要一条路径,而不依赖于返回。