双渲染错误轨道 [英] Double render error rails

查看:31
本文介绍了双渲染错误轨道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道怎么可能得到这个错误:

Not sure how its possible to get this error :

AbstractController::DoubleRenderError users#create

当我在我的控制器中得到这个代码时:

When in my controller I got this code :

render 'new' and return

我从 bugsnag 那里得到了日志,说我在这一行遇到了错误.

I got the log from the bugsnag saying that I got the error at this line.

这是创建方法代码:

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

我在这个控制器中有过滤器:

I have before filter in this 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.实际上,您尝试使控制器动作短路,然后 return redirect_to ....

The reasoning behind it: x and return means x and return nil, thus returns nil. Actually, you try to short-circuit the controller action, and return redirect_to ....

这篇关于双渲染错误轨道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆