gpt4 book ai didi

ruby-on-rails - 双渲染错误轨

转载 作者:数据小太阳 更新时间:2023-10-29 06:48:09 25 4
gpt4 key购买 nike

不确定如何得到这个错误:

AbstractController::DoubleRenderError users#create

在我的 Controller 中,我得到了这段代码:

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

我在这个 Controller 中有之前的过滤器:

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。实际上,您尝试使 Controller Action 短路,然后 return redirect_to ...

关于ruby-on-rails - 双渲染错误轨,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21610090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com