双重渲染错误轨道

时间:2014-02-06 17:39:19

标签: ruby-on-rails ruby

不确定如何获得此错误:

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

4 个答案:

答案 0 :(得分:12)

尝试一下:

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 ...

答案 1 :(得分:5)

并且没有为你做任何事。

在你拥有的每个地方

xxx and return

替换为

xxx
return

对于exameple:

redirect_to signup_path
return

这应该更像你期望的那样

答案 2 :(得分:2)

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

答案 3 :(得分:0)

我认为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

这样很明显,只能使用一条路径,而不依赖于返回。

相关问题